1 package com.protomatter.jdbc.sl; 2 3 52 53 import java.text.*; 54 import java.sql.*; 55 import java.io.*; 56 import java.lang.reflect.*; 57 58 61 class ResultSetUtil 62 { 63 static void formatResultSet(ResultSet s, PrintStream out) 64 throws SQLException 65 { 66 formatResultSet(s, out, false); 67 } 68 69 static void formatResultSet(ResultSet s, PrintStream out, boolean showQuotes) 70 throws SQLException 71 { 72 ResultSetMetaData m = s.getMetaData(); 73 74 int nCols = m.getColumnCount(); 75 int maxcolwidth = 0; 76 for (int i=1; i<=nCols; i++) 77 { 78 String label = m.getColumnLabel(i); 79 if (label == null) label = "[NULL]"; 80 int q = label.length(); 81 if (q > maxcolwidth) 82 maxcolwidth = q; 83 } 84 maxcolwidth += 2; 85 86 int numRows = 0; 87 long time = System.currentTimeMillis(); 88 while (s.next()) 89 { 90 ++numRows; 91 out.println("---------------------------------------------------------"); 92 for (int i=1; i<=nCols; i++) 93 { 94 String label = m.getColumnLabel(i); 95 if (label == null) label = "[NULL]"; 96 out.print(label); 97 for (int j=0; j<(maxcolwidth - label.length()); j++) 98 out.print(" "); 99 try 100 { 101 Object o; 102 if (m.getColumnType(i) == Types.LONGVARBINARY) 104 { 105 o = s.getBytes(i); 106 } 108 else 109 { 110 o = s.getObject(i); 111 } 112 113 if (o instanceof byte[]) 114 { 115 if (showQuotes) out.print("'"); 116 out.print(new String ((byte[])o)); 117 if (showQuotes) out.print("'"); 118 } 119 else if ((o != null) && (o.getClass().getName().equals("oracle.sql.CLOB"))) 120 { 121 try 122 { 123 Class oc = o.getClass(); 124 Method method = oc.getMethod("getCharacterStream", new Class [] { }); 125 Reader r = (Reader)method.invoke(o, new Object [] { }); 126 char buffer[] = new char[1024]; 127 int read = 0; 128 if (showQuotes) out.print("'"); 129 while ((read = r.read(buffer)) != -1) 130 { 131 out.print(new String (buffer, 0, read)); 132 } 133 if (showQuotes) out.print("'"); 134 } 135 catch (Exception x) 136 { 137 if (x instanceof SQLException) 138 throw (SQLException)x; 139 140 System.out.println(""); 141 System.out.println(" --> " + x.toString()); 142 if (showQuotes) out.print("'"); 143 out.print(o.toString()); 144 if (showQuotes) out.print("'"); 145 } 146 } 147 else 148 { 149 if (showQuotes) out.print("'"); 150 out.print(o.toString()); 151 if (showQuotes) out.print("'"); 152 } 153 } 154 catch (NullPointerException e) 155 { 156 out.print("[NULL]"); 157 if (showQuotes) out.print("'"); 158 } 159 out.println(""); 160 } 161 } 162 time = System.currentTimeMillis() - time; 163 out.println("Command returned " + numRows + " rows"); 164 out.println("Getting ResultSet contents took " + time + "ms"); 165 } 166 167 private static String printByteArray(byte[] b) 168 { 169 StringBuffer sb = new StringBuffer (); 170 for (int i=0; i<b.length; i++) 171 { 172 if ((i%20) == 0) sb.append("\n"); 173 DecimalFormat format = new DecimalFormat("000"); 174 sb.append(format.format((int)b[i])); 175 sb.append(" "); 176 } 177 return sb.toString(); 178 } 179 } 180 | Popular Tags |