?? stmtcache1.java
字號(hào):
/* * This sample to demonstrate Implicit Statement Caching. This can be * enabled by calling setStatementCacheSize and setImplicitCachingEnabled(true) * on the Connection Object. * * Please use jdk1.2 or later version * * Please look at the "1. stmt is ..." and "2. stmt is ..." of the * running results. They should point to the same instance (address) * */// You need to import the java.sql package to use JDBCimport java.sql.*;import oracle.jdbc.*;class StmtCache1{ public static void main (String args []) throws SQLException { // Load the Oracle JDBC driver DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); String url = "jdbc:oracle:oci8:@"; try { String url1 = System.getProperty("JDBC_URL"); if (url1 != null) url = url1; } catch (Exception e) { // If there is any security exception, ignore it // and use the default } // Connect to the database Connection conn = DriverManager.getConnection (url, "hr", "hr"); ((OracleConnection)conn).setStatementCacheSize(1); Connection sysconn = DriverManager.getConnection(url, "system", "manager"); String sql = "select FIRST_NAME, LAST_NAME from EMPLOYEES"; System.out.println("Beging of 1st execution"); getOpenCursors (sysconn); // Create a Statement PreparedStatement stmt = conn.prepareStatement (sql); System.out.println("1. Stmt is " + stmt); ((OracleConnection)conn).setImplicitCachingEnabled(true); // Select the FIRST_NAME, LAST_NAME column from the EMPLOYEES table ResultSet rset = stmt.executeQuery (); // Iterate through the result and print the employee names while (rset.next ()) System.out.println (rset.getString (1) + " " + rset.getString (2)); // Close the RseultSet rset.close(); // Close the Statement stmt.close(); System.out.println("End of 1st execution"); getOpenCursors (sysconn); System.out.println("Reexecuting the same SQL"); stmt = conn.prepareStatement (sql); System.out.println("2. Stmt is " + stmt); // Select the FIRST_NAME, LAST_NAME column from the EMPLOYEES table rset = stmt.executeQuery (); // Iterate through the result and print the employee names while (rset.next ()) System.out.println (rset.getString (1) + " " + rset.getString (2)); // Close the RseultSet rset.close(); // Close the Statement stmt.close(); System.out.println("End of 2nd execution"); getOpenCursors (sysconn); // Close the connection conn.close(); System.out.println("After close of connection"); getOpenCursors (sysconn); sysconn.close(); } private static void getOpenCursors (Connection conn) throws SQLException { System.out.println("Open Cusrors are : "); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery ("select SQL_TEXT from V$OPEN_CURSOR"); while (rs.next()) System.out.println("Cursor's sql text is " + rs.getString(1)); rs.close(); rs = null; stmt.close(); stmt = null; }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -