⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transexample.java

📁 这是一本描述JDBC数据库的书籍
💻 JAVA
字号:
/* * TransExample.java * * Created on June 12, 2005, 10:56 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package ch03;import java.sql.*;import javax.sql.*;import java.io.*;import common.*;/** * * @author kevin */public class TransExample {       public static void main(String [] args)  throws Exception{    // create a Connection object named rcConnection    Connection rcConnection = ConnectionFactory.getConnectionFactory().getConnection();        rcConnection.setAutoCommit(false);    // create a Connection object named serConnection    Connection serConnection = ConnectionFactory.getConnectionFactory().getConnection();    serConnection.setAutoCommit(false);    // set transaction isolation level to TRANSACTION_SERIALIZABLE    // for serConnection - serConnection will no longer see    // phantom rows, non-repeatable reads or dirty reads    System.out.println("Setting transaction isolation level to " +      "TRANSACTION_SERIALIZABLE for serConnection");    serConnection.setTransactionIsolation(      oracle.jdbc.OracleConnection.TRANSACTION_SERIALIZABLE    );    // create two Statement objects, named rcStatement and    // serStatement    Statement rcStatement = rcConnection.createStatement();    Statement serStatement = serConnection.createStatement();    // display all products using rcStatement    System.out.println("List of products using rcStatement");    displayProducts(rcStatement);    // display all products using serStatement    System.out.println("List of products using serStatement");    displayProducts(serStatement);    // add product #6 using rcStatement    System.out.println("Adding product #13 " +      "using rcStatement");    rcStatement.executeUpdate(      "INSERT INTO simple_tbl " +      "(id, name) VALUES " +      "(13, 'JDBC Programming')"    );    // update product #1 using rcStatement    System.out.println("Updating product #1's name to New Science " +      "using rcStatement");    rcStatement.executeUpdate(      "UPDATE simple_tbl " +      "SET name = 'New Science' " +      "WHERE id = 1"    );    // commit the changes using rcConnection    System.out.println("Committing changes using rcConnection");    rcConnection.commit();    // display all products using rcStatement    System.out.println("List of products using rcStatement");    displayProducts(rcStatement);    // display all products using serStatement -    // the new product (a phantom row) and the    // modified product name (a non-repeatable read)     // are not visible to serStatement because it has a transaction    // isolation level of TRANSACTION_SERIALIZABLE    System.out.println("List of products using serStatement");    displayProducts(serStatement);    // delete the new product, change the first product name    // back to Modern Science, and commit the changes using    // rcStatement    rcStatement.executeUpdate(      "DELETE FROM simple_tbl " +      "WHERE id = 2"    );    rcStatement.executeUpdate(      "UPDATE simple_tbl " +      "SET name = 'Modern Science' " +      "WHERE id = 1"    );    rcConnection.commit();    // close the JDBC objects    rcStatement.close();    rcConnection.close();    serStatement.close();    serConnection.close();  } // end of main()  private static void displayProducts(    Statement myStatement  ) throws SQLException {    ResultSet productResultSet = myStatement.executeQuery(      "SELECT id, name " +      "FROM simple_tbl"    );    while (productResultSet.next()) {      System.out.println(        productResultSet.getInt("id") + "  " +         productResultSet.getString("name") + " "      );    }    productResultSet.close();  } // end of displayProducts()    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -