Write a java program to create Teacher table(TNo.TName, Sal, Desg) and insert a record in it.

import java.io.*;
import java.sql.*;
public class CreateTeacherTable
{            static Connection cn;
            static Statement st;
            public static void main(String args[])
            {  
                 try
                 {      
                          int tno,sal;
                        String tname,desg;
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
                        st=cn.createStatement();
                       String str="create table Teacher(TNo number,TName varchar(20),Sal number,Desg varchar(20))";
                        st.executeUpdate(str);
                        System.out.println("Table Created");
                        System.out.println("Enter Tno");
                        tno=Integer.parseInt(br.readLine());
                        System.out.println("Enter Tname");
                        tname=br.readLine();
                        System.out.println("Enter Sal");
                        sal=Integer.parseInt(br.readLine());
                        System.out.println("Enter Desg");
                        desg=br.readLine();
                        st.executeUpdate("insert into Teacher values("+tno+",'"+tname+"',"+sal+",'"+desg+"')");
                        System.out.println("Record added successfully");
                        cn.close();      
                        }catch(Exception e)
                               {            
                                      System.out.println(e);          
                               }
            }
}

Comments

Popular posts from this blog

Create a JSP page to accept a number from an user and display it in words: Example: 123 – One Two Three. The output should be in red color.

Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form. (Use PreparedStatement interface)