1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 25 import java.sql.*; 26 27 import org.apache.derby.tools.ij; 28 import org.apache.derby.tools.JDBCDisplayUtil; 29 import org.apache.derbyTesting.functionTests.util.TestUtil; 30 31 public class testRelative { 32 33 static final String NO_CURRENT_ROW_SQL_STATE = 34 (TestUtil.isNetFramework() ? 35 "XJ121" : "24000"); 36 37 public static void main(String [] args) { 38 System.out.println("Test testRelative starting"); 39 Connection con = null; 40 try { 41 ij.getPropertyArg(args); 44 con = ij.startJBMS(); 45 test1(con); 46 } catch (Exception e) 47 { 48 unexpectedException(e); 49 } 50 } 51 52 public static void test1(Connection con) { 53 ResultSet rs = null; 54 PreparedStatement pStmt = null; 55 Statement stmt1 = null; 56 String returnValue = null; 57 58 try 59 { 60 con.setAutoCommit(false); 61 pStmt = con.prepareStatement("create table testRelative(name varchar(10), i int)"); 62 pStmt.executeUpdate(); 63 con.commit(); 64 65 pStmt = con.prepareStatement("insert into testRelative values (?,?)"); 66 67 pStmt.setString(1,"work1"); 68 pStmt.setNull(2,1); 69 pStmt.addBatch(); 70 71 pStmt.setString(1,"work2"); 72 pStmt.setNull(2,2); 73 pStmt.addBatch(); 74 75 pStmt.setString(1,"work3"); 76 pStmt.setNull(2,3); 77 pStmt.addBatch(); 78 79 pStmt.setString(1,"work4"); 80 pStmt.setNull(2,4); 81 pStmt.addBatch(); 82 83 84 pStmt.executeBatch(); 85 con.commit(); 86 } catch(SQLException se) { 87 unexpectedSQLException(se); 88 } catch(Throwable t) { 89 System.out.println("FAIL--unexpected exception: "+t.getMessage()); 90 t.printStackTrace(System.out); 91 } 92 try { 93 testScrolling(ResultSet.CONCUR_READ_ONLY, con); 94 testScrolling(ResultSet.CONCUR_UPDATABLE, con); 95 } catch(Throwable e) { 96 System.out.println("FAIL -- unexpected exception: "+e.getMessage()); 97 e.printStackTrace(System.out); 98 99 } 100 } 101 102 private static void testScrolling(int concurrency, Connection con) 103 throws SQLException 104 { 105 Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, concurrency); 106 ResultSet rs = stmt1.executeQuery("select * from testRelative"); 107 108 rs.next(); System.out.println("Value = " + rs.getString("name")); 110 111 rs.relative(2); 112 System.out.println("Value = " + rs.getString("name")); 113 System.out.println("isFirst = " + rs.isFirst() + 114 " isLast = " + rs.isLast() + 115 " isAfterLast = " + rs.isAfterLast()); 116 rs.relative(-2); 117 System.out.println("Value = " + rs.getString("name")); 118 119 try { 120 rs.relative(10); 121 System.out.println("Value = " + rs.getString("name")); 122 System.out.println("isFirst = " + rs.isFirst() + 123 " isLast = " + rs.isLast() + 124 " isAfterLast = " + rs.isAfterLast()); 125 } catch(SQLException sqle) { 126 127 expectedException(sqle, NO_CURRENT_ROW_SQL_STATE); 128 } 129 } 130 131 139 static private void expectedException (SQLException se, String expectedSQLState) { 140 if( se.getSQLState() != null && (se.getSQLState().equals(expectedSQLState))) { 141 System.out.println("PASS -- expected exception"); 142 } else { 143 System.out.println("FAIL--Unexpected SQLException: " + 144 "SQLSTATE(" +se.getSQLState() + ")" + 145 se.getMessage()); 146 while (se != null) { 147 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se.getMessage()); 148 se.printStackTrace(System.out); 149 se = se.getNextException(); 150 } 151 152 } 153 } 154 155 159 static private void unexpectedSQLException(SQLException se) { 160 System.out.println("FAIL -- Unexpected Exception: "+ 161 "SQLSTATE(" +se.getSQLState() +")" + 162 se.getMessage()); 163 se.printStackTrace(System.out); 164 } 165 166 static private void unexpectedException(Exception e) { 167 System.out.println("FAIL -- Unexpected Exception: "+ 168 e.getMessage()); 169 } 170 171 } 172 | Popular Tags |