📄 stmtclosefuntest.java
字号:
/* Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.StmtCloseFunTest Copyright 2000, 2005 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derbyTesting.functionTests.tests.jdbcapi;import org.apache.derby.tools.ij;import java.sql.Connection;import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.CallableStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Date;import java.sql.Types;import java.util.GregorianCalendar;import org.apache.derby.iapi.reference.JDBC20Translation;import org.apache.derbyTesting.functionTests.util.TestUtil;public class StmtCloseFunTest { static private boolean isDerbyNet = false; public static void main(String[] args) { System.out.println("Statement Close Fun Test starting "); isDerbyNet= TestUtil.isNetFramework(); try { // use the ij utility to read the property file and // make the initial connection. ij.getPropertyArg(args); Connection conn = ij.startJBMS(); test1(conn); test2(conn); test3(conn); conn.close(); } catch (SQLException e) { dumpSQLExceptions(e); } catch (Throwable e) { System.out.println("FAIL -- unexpected exception:" + e.toString()); e.printStackTrace(System.out); } System.out.println("Statement Close Fun Test finished"); } private static void test1(Connection conn) { Statement s; try { System.out.println("Statement test begin"); s = conn.createStatement(); /* We create a table, add a row, and close the statement. */ s.execute("create table tab1(num int, addr varchar(40))"); s.execute("insert into tab1 values (1910,'Union St.')"); s.close(); try { s.executeQuery("create table tab2(num int)"); System.out.println("Statement Test failed (1)"); } catch(SQLException e) { } try { s.executeUpdate("update tab1 set num=180, addr='Grand Ave.' where num=1910"); System.out.println("Statement Test failed"); } catch(SQLException e) { } /* close() is the only method that can be called * after a Statement has been closed, as per * Jon Ellis. */ try { s.close(); } catch(SQLException e) { System.out.println("Statement Test failed (2) " + e.toString()); } try { s.execute("insert into tab1 values (300,'Lakeside Dr.')"); System.out.println("Statement Test failed (3)"); } catch(SQLException e) { } try { s.getMaxFieldSize(); System.out.println("Statement Test failed (4)"); } catch(SQLException e) { } try { s.setMaxFieldSize(100); System.out.println("Statement Test failed (5)"); } catch(SQLException e) { } try { s.getMaxRows(); System.out.println("Statement Test failed (6)"); } catch(SQLException e) { } try { s.setMaxRows(1000); System.out.println("Statement Test failed (7)"); } catch(SQLException e) { } try { s.setEscapeProcessing(true); System.out.println("Statement Test failed (8)"); } catch(SQLException e) { } if (! isDerbyNet) { try { // currently derby only supports returning 0 in this case. int qry_timeout = s.getQueryTimeout(); if (qry_timeout != 0) System.out.println("Statement Test failed, must return 0."); } catch(SQLException e) { System.out.println("Statement Test failed (9) " + e.toString()); } } try { s.setQueryTimeout(20); System.out.println("Statement Test failed (10)"); } catch(SQLException e) { } try { s.cancel(); System.out.println("Statement Test failed (11)"); } catch(SQLException e) { } if (isDerbyNet) System.out.println("beetle 5524"); try { s.getWarnings(); System.out.println("Statement Test failed (12)"); } catch(SQLException e) { } if (isDerbyNet) System.out.println("beetle 5524"); try { s.clearWarnings(); System.out.println("Statement Test failed (13)"); } catch(SQLException e) { } try { s.setCursorName("ABC"); System.out.println("Statement Test failed (14)"); } catch(SQLException e) { } try { s.execute("create table tab3(num int)"); System.out.println("Statement Test failed (15)"); } catch(SQLException e) { } try { s.getResultSet(); System.out.println("Statement Test failed (16)"); } catch(SQLException e) { } try { s.getUpdateCount(); System.out.println("Statement Test failed (17)"); } catch(SQLException e) { } try { s.getMoreResults(); System.out.println("Statement Test failed (18) "); } catch(SQLException e) { } try { s.getResultSetType(); System.out.println("Statement Test failed (19)"); } catch(SQLException e) { } try { s.setFetchDirection(JDBC20Translation.FETCH_FORWARD); System.out.println("Statement Test failed (20)"); } catch(SQLException e) { } try { s.setFetchSize(100); System.out.println("Statement Test failed (21)"); } catch(SQLException e) { } try { s.getFetchSize(); System.out.println("Statement Test failed (22)"); } catch(SQLException e) { } try { s.getResultSetConcurrency(); System.out.println("Statement Test failed (23)"); } catch(SQLException e) { } try { s.addBatch("create table tab3(age int)"); System.out.println("Statement Test failed (24)"); } catch(SQLException e) { } try { s.clearBatch(); System.out.println("Statement Test failed"); } catch(SQLException e) { } try { s.executeBatch(); System.out.println("Statement Test failed"); } catch(SQLException e) { } try { s.getConnection(); System.out.println("Statement Test failed"); } catch(SQLException e) { } } catch (Throwable e) { System.out.println("FAIL -- unexpected exception:" + e.toString()); e.printStackTrace(System.out); return; } System.out.println("Statement test end");} private static void test2(Connection conn) { PreparedStatement ps; try { System.out.println("Prepared Statement test begin"); ps = conn.prepareStatement("create table tab2(a int, b float, c date, d varchar(100))"); ps.execute(); ps.close(); //we test execute() and executeQuery() here ps = conn.prepareStatement("select a from tab2"); ps.execute(); ps.close(); try { ps.execute(); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.executeQuery(); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } ps = conn.prepareStatement("insert into tab2 values(?, ?, ?, ?)"); /* We create a table, add a row, and close the statement. */ ps.setInt(1, 420); ps.setFloat(2, (float)12.21); ps.setDate(3, new Date(1971, 1, 10)); ps.setString(4, "China"); ps.executeUpdate(); ps.close(); //now, we begin the test try { ps.setInt(1, 530); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.setFloat(2, (float)3.14); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.setDate(3, new Date(1975, 11, 15)); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.setDate(3, new Date(1975, 11, 15), new GregorianCalendar()); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.setString(4, "HongKong"); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.executeUpdate(); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } /* close() is the only method that can be called * after a Statement has been closed, as per * Jon Ellis. */ try { ps.close(); } catch(SQLException e) { System.out.println("Prepared Statement Test failed"); } try { ps.clearParameters(); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.getMetaData(); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.getMaxFieldSize(); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.setMaxFieldSize(100); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.getMaxRows(); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.setMaxRows(1000); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } try { ps.setEscapeProcessing(true); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { } if (! isDerbyNet) { try { // currently derby only supports returning 0 in this case. int qry_timeout = ps.getQueryTimeout(); if (qry_timeout != 0) System.out.println("Statement Test failed, must return 0."); } catch(SQLException e) { System.out.println("Statement Test failed"); } } try { ps.setQueryTimeout(20); System.out.println("Prepared Statement Test failed"); } catch(SQLException e) { }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -