1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 import java.sql.SQLException ; 30 import java.sql.Types ; 31 32 import org.apache.derby.tools.ij; 33 import org.apache.derby.tools.JDBCDisplayUtil; 34 35 40 41 public class statementJdbc20 { 42 43 public static void main(String [] args) { 44 Connection con; 45 ResultSet rs; 46 Statement stmt; 47 48 System.out.println("Test statementJdbc20 starting"); 49 50 try 51 { 52 ij.getPropertyArg(args); 55 con = ij.startJBMS(); 56 57 58 stmt = con.createStatement(); 59 60 stmt.execute("create table tab1 (i int, s smallint, r real)"); 62 stmt.executeUpdate("insert into tab1 values(1, 2, 3.1)"); 63 64 stmt.setFetchSize(25); 66 stmt.setFetchDirection(ResultSet.FETCH_REVERSE); 67 stmt.setEscapeProcessing(true); 68 69 try{ 71 stmt.setFetchSize(-1000); 72 } 73 catch(SQLException e){ 74 dumpExpectedSQLExceptions(e); 75 } 76 77 try{ 78 stmt.setFetchDirection(-1000); 79 } 80 catch(SQLException e){ 81 dumpExpectedSQLExceptions(e); 82 } 83 84 85 System.out.println("Fetch Size " + stmt.getFetchSize()); 86 System.out.println("Fetch Direction " + stmt.getFetchDirection()); 87 88 rs = stmt.executeQuery("select * from tab1"); 90 while (rs.next()) 91 { 92 System.out.println(rs.getInt(1) + " " + rs.getShort(2) + 93 " " + rs.getFloat(3)); 94 } 95 96 System.out.println("Result Set Fetch Size " + rs.getFetchSize()); 98 System.out.println("Result Set Fetch Direction " + rs.getFetchDirection()); 99 100 rs.setFetchSize(250); 102 try{ 103 rs.setFetchDirection(ResultSet.FETCH_FORWARD); 104 }catch(SQLException e){ 105 dumpExpectedSQLExceptions(e); 106 } 107 108 System.out.println("Result Set Fetch Size " + rs.getFetchSize()); 109 System.out.println("Result Set Fetch Direction " + rs.getFetchDirection()); 110 111 stmt.setMaxRows(10); 113 try{ 114 rs.setFetchSize(100); 115 } 116 catch(SQLException e){ 117 dumpExpectedSQLExceptions(e); 118 } 119 120 try{ 122 rs.setFetchSize(-2000); 123 } 124 catch(SQLException e){ 125 dumpExpectedSQLExceptions(e); 126 } 127 128 try{ 129 rs.setFetchDirection(-2000); 130 } 131 catch(SQLException e){ 132 dumpExpectedSQLExceptions(e); 133 } 134 135 138 rs.setFetchSize(0); 139 stmt.setFetchSize(0); 140 141 rs.close(); 142 143 try 147 { 148 stmt.executeQuery("create table trash(c1 int)"); 149 } 150 catch (SQLException e) 151 { 152 dumpExpectedSQLExceptions(e); 153 } 154 155 try 157 { 158 rs = stmt.executeQuery("select * from trash"); 159 System.out.println("select from trash expected to fail"); 160 } 161 catch (SQLException e) 162 { 163 dumpExpectedSQLExceptions(e); 164 } 165 166 try 169 { 170 stmt.executeUpdate("values 1"); 171 } 172 catch (SQLException e) 173 { 174 dumpExpectedSQLExceptions(e); 175 } 176 177 stmt.close(); 178 con.close(); 179 180 } 181 catch (SQLException e) { 182 dumpSQLExceptions(e); 183 e.printStackTrace(); 184 } 185 catch (Throwable e) { 186 System.out.println("FAIL -- unexpected exception: "+e); 187 e.printStackTrace(); 188 } 189 190 System.out.println("Test statementJdbc20 finished"); 191 } 192 193 static private void dumpSQLExceptions (SQLException se) { 194 System.out.println("FAIL -- unexpected exception"); 195 while (se != null) { 196 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se); 197 se = se.getNextException(); 198 } 199 } 200 201 static private void dumpExpectedSQLExceptions (SQLException se) { 202 System.out.println("PASS -- expected exception"); 203 while (se != null) { 204 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se); 205 se = se.getNextException(); 206 } 207 } 208 209 } 210 | Popular Tags |