kilam_deelaw

Untitled

Apr 27th, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import java.sql.*;
  2. public class Test {
  3. private String query;
  4. private Statement s;
  5. private Connection c;
  6. private ResultSet rs;
  7. private PreparedStatement ps;
  8. public Test(){
  9.  
  10. this.c=null;
  11. this.ps=null;
  12. this.rs=null;
  13. this.s=null;
  14. query=new String();
  15. }
  16. public void Connection() throws Exception{
  17. Class.forName("com.mysql.jdbc.Driver");
  18. c=DriverManager.getConnection("jdbc:mysql://localhost/db1?user=root");
  19. s=c.createStatement();
  20.  
  21. }
  22. public void Create() throws Exception{
  23.  
  24. query="create table SC(id int not null,name varchar(25),Address varchar(40),Primary Key(id));";
  25. s.executeUpdate(query);
  26. }
  27. public void Insert() throws Exception{
  28. query="insert into SC values(1,'cclass','303-B1')";
  29. s.executeUpdate(query);
  30. }
  31. public void Select() throws Exception{
  32. query= "select *from SC";
  33. rs=s.executeQuery(query);
  34. while(rs.next())
  35. {
  36. System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getString("Address"));
  37. }
  38. }
  39. public void Update() throws Exception{
  40. query="Update SC set name= 'waleed' where id=1;";
  41. s.executeUpdate(query);
  42. }
  43.  
  44. public static void main(String agrs[]) throws Exception{
  45.  
  46. Test t=new Test();
  47. t.Connection();
  48. t.Update();
  49.  
  50. }
  51. }
Add Comment
Please, Sign In to add comment