📄 csprepstmt.java
字号:
conn.close(); System.out.println("csPrepStmt Test Ends"); } catch (Exception e) { e.printStackTrace(); } } // Test creation and execution of many Prepared Statements // Beetle 5130 private static void test5130 (Connection conn) throws Exception { int numOfPreparedStatement = 500; PreparedStatement[] tempPreparedStatement = new PreparedStatement[numOfPreparedStatement]; ResultSet rs; String[] tableName = new String[numOfPreparedStatement]; for (int i = 0; i < numOfPreparedStatement; i++) { tempPreparedStatement[i] = conn.prepareStatement( "SELECT COUNT(*) from SYS.SYSTABLES", ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = tempPreparedStatement[i].executeQuery(); rs.close(); } for (int i = 0; i < numOfPreparedStatement; i++) tempPreparedStatement[i].close(); } private static void test5172(Connection conn) throws Exception { Statement stmt = conn.createStatement(); try { stmt.executeUpdate("drop table tab1"); } catch (SQLException se) { } stmt.executeUpdate( "CREATE TABLE TSTAB (I int, STATUS_TS Timestamp, PROPERTY_TS Timestamp)" ); stmt.executeUpdate("INSERT INTO TSTAB VALUES(1 , '2003-08-15 21:20:00','2003-08-15 21:20:00')"); stmt.executeUpdate("INSERT INTO TSTAB VALUES(2 , '1969-12-31 16:00:00.0', '2003-08-15 21:20:00')"); stmt.close(); String timestamp = "20"; String query = "select STATUS_TS " + "from TSTAB " + "where (STATUS_TS >= ? or " + " PROPERTY_TS<?)"; System.out.println("Negative test setString with Invalid Timestamp:" + timestamp); PreparedStatement ps = conn.prepareStatement(query); ps.setString(1,timestamp); ps.setString(2, timestamp ); try { ResultSet rs = ps.executeQuery(); } catch (SQLException e) { System.out.println("SQLState: " + e.getSQLState() + " message: " + e.getMessage()); } } private static void test4975(Connection conn) throws Exception { BigDecimal minBigDecimalVal = null; BigDecimal rBigDecimalVal = null; String sminBigDecimalVal = null; PreparedStatement pstmt = null; ResultSet rs = null; Statement stmt = null; try { stmt = conn.createStatement(); String createTableSQL = "create table Numeric_Tab (MAX_VAL NUMERIC(30,15), MIN_VAL NUMERIC(30,15), NULL_VAL NUMERIC(30,15))"; // to create the Numeric Table stmt.executeUpdate(createTableSQL); String insertSQL = "insert into Numeric_Tab values(999999999999999,0.000000000000001, null)"; stmt.executeUpdate(insertSQL); //to extract the Maximum Value of BigDecimal to be Updated sminBigDecimalVal = "0.000000000000001"; minBigDecimalVal = new BigDecimal(sminBigDecimalVal); logMsg("Minimum BigDecimal Value: " + minBigDecimalVal); // to update Null value column with Minimum value String sPrepStmt = "update Numeric_Tab set NULL_VAL=?"; // Uncomment and prepare the below statement instead to see JCC bug on setObject for decimal //String sPrepStmt = "update Numeric_Tab set NULL_VAL="+sminBigDecimalVal+" where 0.0 != ?"; logMsg("Prepared Statement String: " + sPrepStmt); // get the PreparedStatement object pstmt = conn.prepareStatement(sPrepStmt); pstmt.setObject(1,minBigDecimalVal); pstmt.executeUpdate(); //to query from the database to check the call of pstmt.executeUpdate //to get the query string String Null_Val_Query = "Select NULL_VAL from Numeric_Tab"; logMsg(Null_Val_Query); rs = stmt.executeQuery(Null_Val_Query); rs.next(); rBigDecimalVal = (BigDecimal) rs.getObject(1); logMsg("Returned BigDecimal Value after Updation: " + rBigDecimalVal); logMsg("Value returned from ctssql.stmt: " + minBigDecimalVal); if(rBigDecimalVal.compareTo(minBigDecimalVal) == 0) { logMsg("setObject Method sets the designated parameter with the Object"); } else { logErr("setObject Method does not set the designated parameter with the Object"); throw new Exception("Call to setObject Method is Failed!"); } } catch(SQLException sqle) { logErr("SQL Exception: " + sqle.getMessage()); throw sqle; } catch(Exception e) { logErr("Unexpected Exception: " + e.getMessage()); throw e; } finally { try { if(rs != null) { rs.close(); rs = null; } if(pstmt != null) { pstmt.close(); pstmt = null; } stmt.executeUpdate("drop table Numeric_Tab"); if(stmt != null) { stmt.close(); stmt = null; } } catch(Exception e){ } } } private static void logErr(String s) { System.err.println(s); } private static void logMsg(String s) { System.out.println(s); } private static void insertTab(String tabname , int numCols) throws SQLException { PreparedStatement ps; System.out.println("insertTab ( " + tabname + "," + numCols + ")" ); String insertSql = "insert into " + tabname + "("; int i; for (i = 1; i < numCols; i++) insertSql += "c"+ i + ", "; insertSql += "c" + i + ") values ( "; for (i = 1; i <= numCols; i++) { insertSql += "?"; if (i != numCols) insertSql += ", "; else insertSql += " )"; } try { ps = conn.prepareStatement(insertSql); //System.out.println("Prepared statement" + insertSql); for (i = 1; i <= numCols; i++) ps.setInt(i,i); ps.executeUpdate(); } catch (SQLException e) { System.out.println("SQLState: " + e.getSQLState() + " message: " + e.getMessage()); //e.printStackTrace(); } } private static void selectFromBigTab() throws SQLException { PreparedStatement ps = null; ResultSet rs = null; String selectSQL = "select * from bigtab"; System.out.println(selectSQL); ps = conn.prepareStatement(selectSQL); rs = ps.executeQuery(); while (rs.next()) { System.out.println("Col # 500 = " + rs.getObject(500) + " Col 1000 = " + rs.getObject(1000)); } rs.close(); ps.close(); } private static String bytesToString(byte[] ba) { String s = null; if (ba == null) return s; s = new String(); for (int i = 0; i < ba.length; i++) s += (Integer.toHexString(ba[i] & 0x00ff)); return s; } // Beetle 5292: Test for LOBs returned as part of a result set. static void testLobInRS(Connection conn) { // Create test objects. try { Statement st = conn.createStatement(); // Clob. st.execute("create table lobCheckOne (c clob(30))"); st.execute("insert into lobCheckOne values (cast " + "('yayorsomething' as clob(30)))"); // Blob. st.execute("create table lobCheckTwo (b blob(30))"); st.execute("insert into lobCheckTwo values (cast " + "( "+ TestUtil.stringToHexLiteral("101010001101") + " as blob(30)))"); } catch (SQLException e) { System.out.println("FAIL: Couldn't create required objects:"); e.printStackTrace(); return; } try { // Clobs. System.out.println("CLOB result."); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from lobCheckOne"); if (rs.next()) System.out.println("GOT ROW: " + rs.getString(1)); else System.out.println("FAIL: Statement executed, but returned " + "an empty result set."); // Blobs. System.out.println("BLOB result."); st = conn.createStatement(); rs = st.executeQuery("select * from lobCheckTwo"); if (rs.next()) System.out.println("GOT ROW: " + rs.getString(1)); else System.out.println("FAIL: Statement executed, but returned " + "an empty result set."); } catch (Exception e) { System.out.println("FAIL: Encountered exception:"); e.printStackTrace(); return; } return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -