以下是示例,該示例使用commit和rollback在JDBC事務教程中進行了描述。
該示例代碼是根據(jù)前幾章中的環(huán)境和數(shù)據(jù)庫設置編寫的。
復制并粘貼以下示例到JDBCExample.java中,如下編譯并運行:
//步驟1.導入所需的軟件包 import java.sql.*; public class JDBCExample { // JDBC驅(qū)動程序名稱和數(shù)據(jù)庫URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // 數(shù)據(jù)庫憑證 static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //步驟2:注冊JDBC驅(qū)動程序 Class.forName("com.mysql.jdbc.Driver"); //步驟3:建立連接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //步驟4:將自動提交設置為false。 conn.setAutoCommit(false); //步驟5:執(zhí)行查詢以創(chuàng)建陳述 // RS示例的必需參數(shù)。 System.out.println("Creating statement..."); stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //步驟6:在“員工”表中插入一行 System.out.println("Inserting one row...."); String SQL = "INSERT INTO Employees " + "VALUES (106, 20, 'Rita', 'Tez')"; stmt.executeUpdate(SQL); //步驟7:在“員工”表中再插入一行 SQL = "INSERT INTO Employees " + "VALUES (107, 22, 'Sita', 'Singh')"; stmt.executeUpdate(SQL); //步驟8:在此處提交數(shù)據(jù)。 System.out.println("Commiting data here...."); conn.commit(); //步驟9:現(xiàn)在列出所有可用的記錄。 String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); System.out.println("List result set for reference...."); printRs(rs); //步驟10:清理環(huán)境 rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //處理JDBC錯誤 se.printStackTrace(); // 如果有錯誤,則回滾更改。 System.out.println("Rolling back data here...."); try{ if(conn!=null) conn.rollback(); }catch(SQLException se2){ se2.printStackTrace(); }//結(jié)束嘗試 }catch(Exception e){ //處理Class.forName的錯誤 e.printStackTrace(); }finally{ //用于關(guān)閉資源 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); }//結(jié)束main public static void printRs(ResultSet rs) throws SQLException{ //確保我們從第一行開始 rs.beforeFirst(); while(rs.next()){ //按列名檢索 int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //顯示值 System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } System.out.println(); }//結(jié)束printRs() }//結(jié)束JDBCExample
現(xiàn)在讓我們編譯上面的示例,如下所示:
C:\>javac JDBCExample.java C:\>
運行時JDBCExample,它將產(chǎn)生以下結(jié)果-
C:\>java JDBCExample Connecting to database... Creating statement... Inserting one row.... Commiting data here.... List result set for reference.... ID: 100, Age: 18, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 30, First: Zaid, Last: Khan ID: 103, Age: 28, First: Sumit, Last: Mittal ID: 106, Age: 20, First: Rita, Last: Tez ID: 107, Age: 22, First: Sita, Last: Singh Goodbye! C:\>