Guest User

Untitled

a guest
Apr 18th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. package xf;
  2. import java.sql.*;
  3. public class zx
  4. {
  5. private String query;
  6. private Connection con;
  7. private Statement state;
  8. private ResultSet rs;
  9. private PreparedStatement prep;
  10. zx()
  11. {
  12. con=null;
  13. state=null;
  14. rs=null;
  15. prep=null;
  16. query= new String();
  17. }
  18.  
  19. public void run()throws Exception
  20. {
  21. Class.forName ("com.mysql.jdbc.Driver");
  22. con=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
  23. state=con.createStatement();
  24. query="Create table SC(id int not null,name varchar(40),primary key(id) )";
  25. state.executeUpdate(query);
  26. }
  27.  
  28. public static void main (String[] args) throws Exception{
  29. zx a=new zx();
  30. a.run();
  31. }
  32. }
  33. //insert after table creation
  34.  
  35. package xf;
  36. import java.sql.*;
  37. public class zx
  38. {
  39. private String query;
  40. private Connection con;
  41. private Statement state;
  42. private ResultSet rs;
  43. private PreparedStatement prep;
  44. zx() throws SQLException, ClassNotFoundException
  45. {
  46. Class.forName ("com.mysql.jdbc.Driver");
  47. con=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
  48. state=con.createStatement();
  49. rs=null;
  50. prep=null;
  51. query= new String();
  52. }
  53.  
  54. public void insert()throws Exception
  55. {
  56. query="insert into SC values(1,'cclass')";
  57. state.executeUpdate(query);
  58. }
  59. public static void main (String[] args) throws Exception{
  60. zx a=new zx();
  61. a.insert();
  62. }
  63. }
  64. //display
  65.  
  66. package xf;
  67. import java.sql.*;
  68. public class zx
  69. {
  70. private String query;
  71. private Connection con;
  72. private Statement state;
  73. private ResultSet rs;
  74. private PreparedStatement prep;
  75. zx() throws SQLException, ClassNotFoundException
  76. {
  77. Class.forName ("com.mysql.jdbc.Driver");
  78. con=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
  79. state=con.createStatement();
  80. rs=null;
  81. prep=null;
  82. query= new String();
  83. }
  84.  
  85. public void get()throws Exception
  86. {
  87. query= "select *from SC";
  88. rs=state.executeQuery(query);
  89. while(rs.next())
  90. {
  91. System.out.println(rs.getInt("id")+""+rs.getString("name"));
  92. }
  93. }
  94. public static void main (String[] args) throws Exception{
  95. zx a=new zx();
  96. a.get();
  97. }
  98. }
Add Comment
Please, Sign In to add comment