📄 lobtest.java
字号:
System.out.println("\tOUT = Object : "+prettyType(c)); } else System.out.println("\tOUT = '"+v+"' : "+c.getClass().getName()); } } } } public ResultSet X(String sql) throws SQLException { try { System.out.println("\n"+sql); // cercumwait stupid executeQuery which can't take non-selects... boolean result = ( (sql.charAt(0) == 'S') || (sql.charAt(0) == 's')); // detect "select" which returns result.. if (!result) { st.execute(sql); } else { return st.executeQuery(sql); } } catch (Throwable e) { LOBTest.printException(e); } return null; } public void Xprint(String sql) { try { ResultSet rs = X(sql); printResultSet(rs); } catch (Throwable e) { LOBTest.printException(e); } } static String[] getterName = { "getObject", "getArray", "getAsciiStream", // 2 "getBigDecimal", "getBinaryStream", "getBlob", // 5 "getBoolean", "getByte", "getBytes", // 8 "getCharacterStream", "getClob", "getDate", // 11 "getDouble", "getFloat", "getInt", "getLong", // 15 "getRef", "getShort", "getString", "getTime", // 19 "getTimeStamp", "getURL" // 21 }; // getter takes a RESULTSET and uses GETTER on COLumn // getters numbered 0..N-1, for N-1 null is returned // otherwise descriptive string is returned // if the getter throws exception the string says so public static String getter(ResultSet rs, int getter, int col) { Object o = "-NO VALUE-"; String s = ""; try { if (getter < getterName.length) { // avoid array exception s = getterName[getter]; for(int i=s.length(); i<20; i++) s+=' '; s += " ->"; } switch(getter) { case 0: {o = rs.getObject(col); break;} case 1: {Array v=rs.getArray(col);o=v;break;} case 2: {InputStream v=rs.getAsciiStream(col);o=v;break;} case 3: {BigDecimal v=rs.getBigDecimal(col);o=v;break;} case 4: {InputStream v=rs.getBinaryStream(col);o=v;break;} case 5: {Blob v=rs.getBlob(col);o=v;break;} case 6: {boolean v=rs.getBoolean(col);o=new Boolean(v);break;} case 7: {byte v=rs.getByte(col);o=new Byte(v);break;} case 8: {byte[] v=rs.getBytes(col);o=v;break;} case 9: {Reader v=rs.getCharacterStream(col);o=v;break;} case 10:{Clob v=rs.getClob(col);o=v;break;} case 11:{Date v=rs.getDate(col);o=v; break;} case 12:{double v=rs.getDouble(col);o=new Double(v);break;} case 13:{float v=rs.getFloat(col);o=new Float(v);break;} case 14:{int v=rs.getInt(col);o=new Integer(v);break;} case 15:{long v=rs.getLong(col);o=new Long(v);break;} case 16:{Ref v=rs.getRef(col);o=v;break;} case 17:{short v=rs.getShort(col);o=new Short(v);break;} case 18:{String v=rs.getString(col);o=v;break;} case 19:{Time v=rs.getTime(col);o=v;break;} case 20:{Timestamp v=rs.getTimestamp(col);o=v;break;}// case 21:{URL v=rs.getURL(col);o=v;break;} default: return null; } // fixup if it contains classname (remove "random" part after @) String v = o.toString(); if (v.indexOf('@') != -1) { // non standard java object. s += "Object' \t: "+prettyType(o); } else { // default stringifier... s += "'"+v+"' \t: "+o.getClass().getName(); } } catch (Throwable e) { s += "\t\tEXCEPTION ("+e.getMessage()+")"; } return s; } static public String prettyType(Object o) { if (o instanceof java.sql.Blob) return "java.sql.Blob"; if (o instanceof java.sql.Clob) return "java.sql.Clob"; if (o instanceof java.io.InputStream) return "java.io.InputStream"; if (o instanceof java.io.Reader) return "java.io.Reader"; if (o instanceof byte[]) return "byte[]"; return "Unknown type - "+o.getClass().getName(); } public void testGetters() throws SQLException { for(int i=0; i<columns; i++) { System.out.println("\n\n=== Columntype "+colTypes[i]); String s = "select "+colNames[i]+" from "+table; ResultSet rs = X(s); rs.next(); // goto first int getno = 0; String r; while(null!=(r = getter(rs, getno, 1))) { System.out.println("\t"+i+" "+r); getno++; } } } public void testMetaData() { System.out.println("\n\n---< METADATA TESTS\n"); // plain select for(int i=0; i<columns; i++) { String s = "select "+colNames[i]+" from "+table; Xprint(s); } } public void testCastTo() { System.out.println("\n\n---< type CAST TO types: METADATA TESTS\n"); // CAST ( column TO types ) for(int i=0; i<columns; i++) { String s; if (colTypes[i].startsWith("bit")) s = "select cast( typecol as char (8) for bit data) from "+table; else s = "select cast( typecol as "+colTypes[i]+" ) from "+table; Xprint(s); } } public void testCastFrom() { System.out.println("\n\n---< columns CAST TO type: METADATA TESTS\n"); // CAST ( coltypes TO type ) for(int i=0; i<columns; i++) { String s; if (typeName.startsWith("bit")) { s = "select cast( "+colNames[i]+" as char (8) for bit data ) from "+table; } else s = "select cast( "+colNames[i]+" as "+typeName+typeSpec+" ) from "+table; Xprint(s); } } public void testBlobInsert() { System.out.println("\n\n---< BLOB Insertion Tests\n"); // create table for testing { Xprint("create table blobCheck (bl blob(80)) "); } // test insertion of literals. for (int i=0; i < columns; i++) { if (colTypes[i].indexOf("blob") == -1) continue; // Check char literals. // (fail) String insert = "insert into blobCheck (bl" + " ) values ('string' )"; Xprint(insert); // (succeed) insert = "insert into blobCheck (bl" + " ) values (cast (" + TestUtil.stringToHexLiteral("string") + " as blob(80)) )"; Xprint(insert); // Check bit literals. // (fail) insert = "insert into blobCheck (bl" + " ) values (X'48' )"; Xprint(insert); // old CS compatible value: ( b'01001' ) // (succeed) insert = "insert into blobCheck (bl" + " ) values (cast (X'C8' as blob(80)) )"; Xprint(insert); // Check hex literals. // (fail) insert = "insert into blobCheck (bl" + " ) values ( X'a78a' )"; Xprint(insert); // (succeed) insert = "insert into blobCheck (bl" + " ) values (cast (X'a78a' as blob(80)) )"; Xprint(insert); } } public void test() throws SQLException { // create table for testing { String create = "create table "+table+" ( dummy int "; for(int i=0; i<columns; i++) { create += ", "+colNames[i]+" "+colTypes[i]; } create += " )"; Xprint(create); //st.execute(create); } // insert one row of numbers in string format if possible. { String insert = "insert into "+table+" values ( 45 "; for(int i=0; i<columns; i++) { insert += "," + colData[i] ; } insert += " )"; Xprint(insert); } // insert various data in various columns, some will fail (int) { for(int i=0; i<columns; i++) { String insert = "insert into "+table+" ( "+colNames[i]; if (isBitColumn(i)) // have to cast for blob columns. { insert += " ) values cast ( " + TestUtil.stringToHexLiteral("true") + " AS " + colTypes[i] + ")"; } else insert += " ) values ( 'true' )"; Xprint(insert); } } // run tests testGetters(); testMetaData(); testCastFrom(); testCastTo(); // cleanup Xprint("drop table "+table); //st.execute("drop table "+table); } private boolean isBitColumn(int offset) { return ((offset == BLOB_OFFSET) || (offset == BIT_OFFSET) || (offset == LONG_VARBINARY_OFFSET) || (offset == TYPE_COL_OFFSET) ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -