Previn

Insert_db

Sep 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1.  
  2. package test_database;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8.  
  9.  
  10. public class Insert_db {
  11.    
  12.      public static void main(String []arg)
  13.      {
  14.                      
  15.         Connection con;
  16.         PreparedStatement ps;
  17.         ResultSet rs;        
  18.        
  19.         try
  20.         {
  21.             Class.forName("com.mysql.jdbc.Driver");
  22.             con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
  23.             System.out.println("Database Connected");      
  24.            
  25.             //Insert query
  26.             ps=con.prepareStatement("insert into test_db (Name , Rollno , Age) values (?,?,?)");
  27.             ps.setString(1, "Previnkumar");
  28.             ps.setString(2, "15CS068");
  29.             ps.setString(3, "18");
  30.             ps.executeUpdate();
  31.             System.out.println("Inserted Successfully");
  32.  
  33.         }
  34.         catch(Exception e)
  35.         {
  36.             System.out.println(e);
  37.         }
  38.  
  39.     }
  40.          
  41. }
Add Comment
Please, Sign In to add comment