编辑这个页面须要登录或更高权限!
以下是示例,该示例使用setSavepoint和rollback在事务教程中进行了描述。
该示例代码是根据前几章中的环境和数据库设置编写的。
复制并粘贴以下示例到JDBCExample.java中,如下编译并运行:
//步骤1.导入所需的软件包 import java.sql.*; public class JDBCExample { // JDBC驱动程序名称和数据库URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // 数据库凭证 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驱动程序 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:执行查询以删除陈述 // RS示例的必需参数。 System.out.println("Creating statement..."); stmt = conn.createStatement(); //步骤6:现在列出所有可用的记录。 String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); System.out.println("List result set for reference...."); printRs(rs); // 步骤7:删除ID大于104的行 // 但是在这样做之前要先保存点。 Savepoint savepoint1 = conn.setSavepoint("ROWS_DELETED_1"); System.out.println("Deleting row...."); String SQL = "DELETE FROM Employees " + "WHERE ID = 110"; stmt.executeUpdate(SQL); // 糟糕...我们删除了太错误的员工! //步骤8:在保存点2之后回滚更改。 conn.rollback(savepoint1); // 步骤9:删除ID大于104的行 // 但是在这样做之前要先保存点。 Savepoint savepoint2 = conn.setSavepoint("ROWS_DELETED_2"); System.out.println("Deleting row...."); SQL = "DELETE FROM Employees " + "WHERE ID = 95"; stmt.executeUpdate(SQL); //步骤10:现在列出所有可用的记录。 sql = "SELECT id, first, last, age FROM Employees"; rs = stmt.executeQuery(sql); System.out.println("List result set for reference...."); printRs(rs); //步骤10:清理环境 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(); }//结束try }catch(Exception e){ //处理Class.forName的错误 e.printStackTrace(); }finally{ //用于关闭资源 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } }//结束try System.out.println("Goodbye!"); }//结束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(); }//结束printRs() }//结束JDBCExample
现在,让我们编译上面的示例,如下所示:
C:\>javac JDBCExample.java C:\>
运行时JDBCExample,它将产生以下结果-
C:\>java JDBCExample Connecting to database... Creating statement... List result set for reference.... ID: 95, Age: 20, First: Sima, Last: Chug 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: 30, First: Sumit, Last: Mittal ID: 110, Age: 20, First: Sima, Last: Chug Deleting row.... Deleting row.... 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: 30, First: Sumit, Last: Mittal ID: 110, Age: 20, First: Sima, Last: Chug Goodbye! C:\>