Previn

Select_db

Sep 18th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1.  
  2.  
  3. package test_database;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9.  
  10. public class Select_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.             //Update query
  26.             ps=con.prepareStatement("select * from test_db where Name=?");
  27.             ps.setString(1, "Previnkumar");
  28.             rs=ps.executeQuery();
  29.             while(rs.next())
  30.             {
  31.                 System.out.println(rs.getString(1));
  32.                 System.out.println(rs.getString(2));
  33.                 System.out.println(rs.getInt(3));
  34.             }
  35.  
  36.  
  37.         }
  38.         catch(Exception e)
  39.         {
  40.             System.out.println(e);
  41.         }
  42.  
  43.     }    
  44. }
Add Comment
Please, Sign In to add comment