harsh428

Untitled

Sep 1st, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3. public class JDBC1_2
  4. {
  5. static final String JDBC_DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
  6. static final String DB_URL="jdbc:odbc:jdbc";
  7. static final String USER="system";
  8. static final String PASS="admin";
  9. public static void main(String[]args)
  10. {
  11. Connection conn=null;
  12. Statement stmt=null;
  13. try
  14. {
  15. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  16. conn=DriverManager.getConnection(DB_URL,USER,PASS);
  17. stmt=conn.createStatement();
  18.  
  19. String csql="Create Table database17 (id INTEGER not NULL,Name VARCHAR(30),Age INTEGER);";
  20. stmt.executeUpdate(csql);
  21. System.out.println("Table Created");
  22.  
  23. String insql="INSERT INTO database17 VALUES(1,'Bhavesh',20);";
  24. stmt.executeUpdate(insql);
  25. System.out.println("One record inserted");
  26. insql="INSERT INTO database17 VALUES(2,'Mahesh',20);";
  27. stmt.executeUpdate(insql);
  28. System.out.println("Second record inserted");
  29. insql="INSERT INTO database17 VALUES(3,'Shubham',20);";
  30. stmt.executeUpdate(insql);
  31. System.out.println("Third record inserted");
  32. insql="INSERT INTO database17 VALUES(4,'Ajit',20);";
  33. stmt.executeUpdate(insql);
  34. System.out.println("Fourth record inserted");
  35.  
  36. System.out.println("----------------------");
  37. System.out.println("ID NAME AGE");
  38. System.out.println("----------------------");
  39. String sql1="select * from database17;";
  40. ResultSet result1 = stmt.executeQuery(sql1);
  41. while(result1.next())
  42. {
  43. System.out.println(result1.getInt(1)+ " " +result1.getString(2)+ " " +result1.getInt(3));
  44. }
  45.  
  46.  
  47.  
  48. String upsql="UPDATE database17 SET NAME='Maddy' WHERE id=2;";
  49. stmt.executeUpdate(upsql);
  50. System.out.println("Record Updated");
  51.  
  52. System.out.println("Records After Updated");
  53. System.out.println("----------------------");
  54. System.out.println("ID NAME AGE");
  55. System.out.println("----------------------");
  56. String sql="select * from database17;";
  57. ResultSet result = stmt.executeQuery(sql);
  58. while(result.next())
  59. {
  60. System.out.println(result.getInt(1)+ " " +result.getString(2)+ " " +result.getInt(3));
  61. }
  62.  
  63. String delsql="drop table database17;";
  64. stmt.executeUpdate(delsql);
  65. System.out.println("Table deleted");
  66.  
  67. }
  68. catch(SQLException ex)
  69. {
  70. ex.printStackTrace();
  71. }
  72. catch(Exception e)
  73. {
  74. e.printStackTrace();
  75. }
  76. finally
  77. {
  78. try
  79. {
  80. if(stmt!=null)
  81. conn.close();
  82. }
  83. catch(SQLException se)
  84. {
  85. se.printStackTrace();
  86. }
  87. }
  88. }
  89. }
Add Comment
Please, Sign In to add comment