1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 import java.sql.Connection ; 24 import java.sql.DriverManager ; 25 import java.sql.ResultSetMetaData ; 26 import java.sql.ResultSet ; 27 import java.sql.Statement ; 28 import java.sql.SQLException ; 29 import java.sql.Types ; 30 import java.sql.PreparedStatement ; 31 import java.sql.CallableStatement ; 32 import java.sql.DatabaseMetaData ; 33 import java.io.*; 34 35 import org.apache.derby.tools.ij; 36 import org.apache.derby.tools.JDBCDisplayUtil; 37 import org.apache.derbyTesting.functionTests.util.TestUtil; 38 39 43 44 public class connectionJdbc20{ 45 46 static private boolean isDerbyNet = false; 47 48 static private String [] testObjects = {"TABLE TAB1"}; 49 50 public static void main(String [] args) { 51 Connection conn, connreturn; 52 Statement stmt, stmtreturn; 53 54 55 System.out.println("Test connection20 starting"); 56 try 57 { 58 ij.getPropertyArg(args); 61 conn = ij.startJBMS(); 62 isDerbyNet = TestUtil.isNetFramework(); 63 stmt = conn.createStatement(); 64 TestUtil.cleanUpTest(stmt, testObjects); 66 stmt.execute("create table tab1("+ 68 "c1 char(100) for bit data,"+ 69 "c2 varchar(100) for bit data," + 70 "c3 long varchar for bit data,"+ 71 "c4 char(100),"+ 72 "c5 varchar(100),"+ 73 "c6 long varchar)"); 74 75 connreturn = stmt.getConnection(); 76 if (conn.equals(connreturn)) 77 System.out.println("Got Same Connection Object"); 78 else 79 System.out.println("Got Different Connection Object"); 80 81 load_data(connreturn); 83 84 ResultSet rs = stmt.executeQuery("select " + 86 "c1," + 87 "c2," + 88 "c3," + 89 "c4," + 90 "c5," + 91 "c6," + 92 "c1 as c1_spare," + 93 "c2 as c2_spare," + 94 "c3 as c3_spare " + 95 "from tab1"); 96 int loop = 0; 97 while(loop < 2 ) 98 { 99 while (rs.next()) 100 { 101 for(int i=1 ; i < 7 ; i++) 102 { 103 get_using_object(rs, i); 104 get_using_string(rs, i); 105 106 get_using_ascii_stream(rs, i); 107 108 if(i < 4 ) { 110 get_using_binary_stream(rs, i + 6); 111 get_using_bytes(rs, i + 6); 112 } 113 } 114 } 115 stmtreturn = rs.getStatement(); 117 if (stmt.equals(stmtreturn)) 118 System.out.println("Got Same Statement Object"); 119 else 120 System.out.println("Got Different Statement Object"); 121 122 rs.close(); 123 rs = stmt.executeQuery("select " + 124 "c1," + 125 "c2," + 126 "c3," + 127 "c4," + 128 "c5," + 129 "c6," + 130 "c1 as c1_spare," + 131 "c2 as c2_spare," + 132 "c3 as c3_spare " + 133 "from tab1"); 134 loop++; 135 } 136 137 stmt.close(); 138 139 DatabaseMetaData dbmeta = conn.getMetaData(); 141 142 rs = dbmeta.getTypeInfo(); 143 while (rs.next()) 144 { 145 System.out.println(rs.getString(1)); 146 } 147 stmt = rs.getStatement(); 149 150 System.out.println("Try to Get the connection back from metadata"); 152 connreturn = dbmeta.getConnection(); 153 if (conn.equals(connreturn)) 154 System.out.println("Got Same Connection Object"); 155 else 156 System.out.println("Got Different Connection Object"); 157 158 159 CallableStatement cs = conn.prepareCall("select * from tab1"); 161 System.out.println(" Try to get the connection back from a callable stmt"); 162 connreturn = cs.getConnection(); 163 if (conn.equals(connreturn)) 164 System.out.println("Got Same Connection Object"); 165 else 166 System.out.println("Got Different Connection Object"); 167 168 cs.close(); 169 conn.close(); 170 TestUtil.cleanUpTest(stmt, testObjects); 171 172 } 173 catch (SQLException e) { 174 dumpSQLExceptions(e); 175 e.printStackTrace(); 176 } 177 catch (Throwable e) { 178 System.out.println("FAIL -- unexpected exception: "+e); 179 e.printStackTrace(); 180 } 181 182 System.out.println("Test getConnection finished"); 183 } 184 185 186 static private void load_data(Connection conn) throws Exception { 187 PreparedStatement pstmt = null; 188 189 try{ 190 pstmt = conn.prepareStatement( 191 "insert into tab1 values(?,?,?,?,?,?)"); 192 String c1_value="C1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 193 String c2_value="C2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 194 String c3_value="C3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 195 String c4_value="C4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 196 String c5_value="C5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 197 String c6_value="C6XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 198 199 pstmt.setBytes(1, c1_value.getBytes("US-ASCII")); 200 pstmt.setBytes(2, c2_value.getBytes("US-ASCII")); 201 pstmt.setBytes(3, c3_value.getBytes("US-ASCII")); 202 pstmt.setString(4, c4_value); 203 pstmt.setString(5, c5_value); 204 pstmt.setString(6, c6_value); 205 pstmt.execute(); 206 207 System.out.println("Try to get connection using preaparedstatement"); 209 Connection connreturn = pstmt.getConnection(); 210 if (conn.equals(connreturn)) 211 System.out.println("Got Same Connection Object"); 212 else 213 System.out.println("Got Different Connection Object"); 214 215 }catch(SQLException e){ 216 dumpSQLExceptions(e); 217 e.printStackTrace(); 218 }catch(Throwable e ){ 219 System.out.println("Fail -- unexpected exception "); 220 e.printStackTrace(); 221 }finally{ 222 pstmt.close(); 223 } 224 } 225 226 static private void dumpSQLExceptions (SQLException se) { 227 System.out.println("FAIL -- unexpected exception"); 228 while (se != null) { 229 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se); 230 se = se.getNextException(); 231 } 232 } 233 234 static private void dumpExpectedSQLExceptions (SQLException se) { 235 System.out.println("PASS -- expected exception"); 236 while (se != null) { 237 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se); 238 se = se.getNextException(); 239 } 240 } 241 242 static private int printbytearray(byte [] a , int len, int count){ 243 244 for (int i =0 ; i < len; i++, count++) { 245 246 System.out.print("x" + Integer.toHexString(a[i])); 247 248 if ((i > 0) && ((i % 20) == 0)) 249 System.out.println(""); 250 } 251 return count; 252 } 253 254 static void get_using_object(ResultSet rs, int col_no) throws Exception { 255 System.out.println("getObject(" + col_no + ")"); 256 Object cobj = rs.getObject(col_no); 257 if (cobj instanceof byte[]){ 258 byte[] bytearray = (byte[]) cobj; 259 System.out.println(" as byte[] length " + bytearray.length); 260 printbytearray(bytearray, bytearray.length, 0); 261 System.out.println(""); 262 }else { 263 System.out.println(" as String"); 264 System.out.println(cobj.toString()); 265 } 266 } 267 268 static void get_using_bytes(ResultSet rs, int col_no) throws Exception { 269 System.out.println("getBytes(" + col_no + ")"); 270 byte[] bytearray = (byte[]) rs.getBytes(col_no); 271 printbytearray(bytearray, bytearray.length, 0); 272 System.out.println(""); 273 } 274 275 static void get_using_string(ResultSet rs, int col_no) throws Exception { 276 String s = rs.getString(col_no); 277 System.out.println("getString(" + col_no + ") length " + s.length()); 278 System.out.println(s); 279 } 280 281 static void get_using_ascii_stream(ResultSet rs, int col_no) throws Exception { 282 System.out.println("getAsciiStream(" + col_no + ")"); 283 int no_bytes_read = 0; 284 InputStream rsbin = rs.getAsciiStream(col_no); 285 byte [] bytearray = new byte[200]; 286 int count = 0; 287 while((no_bytes_read=rsbin.read(bytearray)) != -1) 288 { 289 count = printbytearray(bytearray, no_bytes_read, count); 290 } 291 System.out.println(""); 292 293 } 294 295 static void get_using_binary_stream(ResultSet rs, int col_no) throws Exception { 296 System.out.println("getBinaryStream(" + col_no + ")"); 297 int no_bytes_read = 0; 298 InputStream rsbin = rs.getBinaryStream(col_no); 299 byte [] bytearray = new byte[200]; 300 int count = 0; 301 while((no_bytes_read=rsbin.read(bytearray)) != -1) 302 { 303 count = printbytearray(bytearray, no_bytes_read, count); 304 } 305 System.out.println(""); 306 } 307 308 } 309 | Popular Tags |