当前位置:Gxlcms > 数据库问题 > JDBC-setSavepoint,releaseSavepoint例子

JDBC-setSavepoint,releaseSavepoint例子

时间:2021-07-01 10:21:17 帮助过:6人阅读



示例代码基于之前设置的环境和数据库编写。

复制粘贴代码到JDBCExample.java,编译运行如下:
复制纯文本新窗口
  1. //STEP 1. Import required packages
  2. import java.sql.*;
  3. public class JDBCExample {
  4. // JDBC driver name and database URL
  5. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  6. static final String DB_URL = "jdbc:mysql://localhost/EMP";
  7. // Database credentials
  8. static final String USER = "username";
  9. static final String PASS = "password";
  10. public static void main(String[] args) {
  11. Connection conn = null;
  12. Statement stmt = null;
  13. try{
  14. //STEP 2: Register JDBC driver
  15. Class.forName("com.mysql.jdbc.Driver");
  16. //STEP 3: Open a connection
  17. System.out.println("Connecting to database...");
  18. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  19. //STEP 4: Set auto commit as false.
  20. conn.setAutoCommit(false);
  21. //STEP 5: Execute a query to delete statment with
  22. // required arguments for RS example.
  23. System.out.println("Creating statement...");
  24. stmt = conn.createStatement();
  25. //STEP 6: Now list all the available records.
  26. String sql = "SELECT id, first, last, age FROM Employees";
  27. ResultSet rs = stmt.executeQuery(sql);
  28. System.out.println("List result set for reference....");
  29. printRs(rs);
  30. // STEP 7: delete rows having ID grater than 104
  31. // But save point before doing so.
  32. Savepoint savepoint1 = conn.setSavepoint("ROWS_DELETED_1");
  33. System.out.println("Deleting row....");
  34. String SQL = "DELETE FROM Employees " +
  35. "WHERE ID = 110";
  36. stmt.executeUpdate(SQL);
  37. // oops... we deleted too wrong employees!
  38. //STEP 8: Rollback the changes afetr save point 2.
  39. conn.rollback(savepoint1);
  40. // STEP 9: delete rows having ID grater than 104
  41. // But save point before doing so.
  42. Savepoint savepoint2 = conn.setSavepoint("ROWS_DELETED_2");
  43. System.out.println("Deleting row....");
  44. SQL = "DELETE FROM Employees " +
  45. "WHERE ID = 95";
  46. stmt.executeUpdate(SQL);
  47. //STEP 10: Now list all the available records.
  48. sql = "SELECT id, first, last, age FROM Employees";
  49. rs = stmt.executeQuery(sql);
  50. System.out.println("List result set for reference....");
  51. printRs(rs);
  52. //STEP 10: Clean-up environment
  53. rs.close();
  54. stmt.close();
  55. conn.close();
  56. }catch(SQLException se){
  57. //Handle errors for JDBC
  58. se.printStackTrace();
  59. // If there is an error then rollback the changes.
  60. System.out.println("Rolling back data here....");
  61. try{
  62. if(conn!=null)
  63. conn.rollback();
  64. }catch(SQLException se2){
  65. se2.printStackTrace();
  66. }//end try
  67. }catch(Exception e){
  68. //Handle errors for Class.forName
  69. e.printStackTrace();
  70. }finally{
  71. //finally block used to close resources
  72. try{
  73. if(stmt!=null)
  74. stmt.close();
  75. }catch(SQLException se2){
  76. }// nothing we can do
  77. try{
  78. if(conn!=null)
  79. conn.close();
  80. }catch(SQLException se){
  81. se.printStackTrace();
  82. }//end finally try
  83. }//end try
  84. System.out.println("Goodbye!");
  85. }//end main
  86. public static void printRs(ResultSet rs) throws SQLException{
  87. //Ensure we start with first row
  88. rs.beforeFirst();
  89. while(rs.next()){
  90. //Retrieve by column name
  91. int id = rs.getInt("id");
  92. int age = rs.getInt("age");
  93. String first = rs.getString("first");
  94. String last = rs.getString("last");
  95. //Display values
  96. System.out.print("ID: " + id);
  97. System.out.print(", Age: " + age);
  98. System.out.print(", First: " + first);
  99. System.out.println(", Last: " + last);
  100. }
  101. System.out.println();
  102. }//end printRs()

人气教程排行