Java operation of the database - a database using JDBC connection

Java operation of the database - used in JDBC affairs

Abstract: This paper learned how to use JDBC transactions inside.

Use transaction control method of the Connection

When the procedure for obtaining a JDBC Connection object to the database, the Connection object is automatically submit SQL statements sent to the database by default. To turn off this default submission, which allows multiple SQL execution in a transaction, JDBC provides transaction control method can be used.

Common method

Automatic status inquiry submitted: boolean getAutoCommit () throws SQLException;

Status is set automatically submitted: void setAutoCommit (boolean autoCommit) throws SQLException;

Set Restore Point: Savepoint setSavepoint () throws SQLException;

Setting specifies the name of the restore point: Savepoint setSavepoint (String name) throws SQLException;

Delete the specified name restore points: void releaseSavepoint (Savepoint savepoint) throws SQLException;

提交:void commit() throws SQLException;

回滚:void rollback() throws SQLException;

Rollback to specify the name of the restore point: void rollback (Savepoint savepoint) throws SQLException;

Query isolation level: int getTransactionIsolation () throws SQLException;

Setting the isolation level: void setTransactionIsolation (int level) throws SQLException;

Use Case

 1 public static void main(String[] args) {
 2     try {
 3         Class.forName("com.mysql.jdbc.Driver");
 4     } catch (ClassNotFoundException e) {
 5         e.printStackTrace();
 6     }
 7     Connection conn = null;
 8     PreparedStatement pstmt = null;
 9     try {
10         String url = "jdbc:mysql://192.168.35.128:3306/demo";
11         String user = "root";
12         String password = "123456";
13         conn = DriverManager.getConnection(url, user, password);
14         boolean status = conn.getAutoCommit();
15         System.out.println("AutoCommit = " + status);
16         conn.setAutoCommit(false);
17         pstmt = conn.prepareStatement("delete from student where id = 906");
18         pstmt.executeUpdate();
19         int e = 1 / 0;// 模拟出现异常
20         pstmt = conn.prepareStatement("delete from student where id = 907");
21         pstmt.executeUpdate ();
 22 is          conn.commit (); // submitted manually, can not be omitted 
23 is      } the catch (Exception E) {
 24          e.printStackTrace ();
 25          the try {
 26 is              conn.rollback (); // manual rollback , may be omitted, the system will automatically roll back 
27          } the catch (SQLException EX) {
 28              ex.printStackTrace ();
 29          }
 30      } the finally {
 31 is          the try {
 32              pstmt.close ();
 33 is          } the catch (SQLException e) {
34             e.printStackTrace();
35         }
36         try {
37             conn.close();
38         } catch (SQLException e) {
39             e.printStackTrace();
40         }
41     }
42 }

Guess you like

Origin www.cnblogs.com/shamao/p/11917928.html