JDBC Example in Java for inserting data in data base

 import java.sql.*;  
 import java.io.*;  
 class InsertDB  
 {  
      public static void main(String[] args)throws ClassNotFoundException,SQLException,IOException   
      {  
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
           int st1=0;   
           String name=null, adress=null;  
           System.out.println("Enter Student No:");  
           String n1=br.readLine();  
           st1=Integer.parseInt(n1.trim());  
           System.out.println("Enter Student Name:");  
           name=br.readLine().trim();  
           System.out.println("Enter student Address:");  
           adress=br.readLine().trim();  
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
           Connection con=DriverManager.getConnection("jdbc:odbc:MySqlDSN");  
           Statement st=con.createStatement();  
           String status="insert into student_info values ("+st1+",'"+name+"','"+adress+"')";  
           st.executeUpdate(status);  
           ResultSet rs=st.executeQuery("select * from student_info");  
           while (rs.next())  
           {  
                System.out.println(rs.getString(1)+" "+rs.getString(2) +rs.getString(3));  
           }  
           rs.close();  
           con.commit();  
           st.close();  
           con.close();  
      }  
 }  

Post a Comment

0 Comments