001: package com.protomatter.jdbc.sl;
002:
003: /**
004: * {{{ The Protomatter Software License, Version 1.0
005: * derived from The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed for the
024: * Protomatter Software Project
025: * (http://protomatter.sourceforge.net/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Protomatter" and "Protomatter Software Project" must
030: * not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact support@protomatter.com.
033: *
034: * 5. Products derived from this software may not be called "Protomatter",
035: * nor may "Protomatter" appear in their name, without prior written
036: * permission of the Protomatter Software Project
037: * (support@protomatter.com).
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
043: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE. }}}
051: */
052:
053: import java.text.*;
054: import java.sql.*;
055: import java.io.*;
056: import java.lang.reflect.*;
057:
058: /**
059: * A utility class for formatting JDBC ResultSets.
060: */
061: class ResultSetUtil {
062: static void formatResultSet(ResultSet s, PrintStream out)
063: throws SQLException {
064: formatResultSet(s, out, false);
065: }
066:
067: static void formatResultSet(ResultSet s, PrintStream out,
068: boolean showQuotes) throws SQLException {
069: ResultSetMetaData m = s.getMetaData();
070:
071: int nCols = m.getColumnCount();
072: int maxcolwidth = 0;
073: for (int i = 1; i <= nCols; i++) {
074: String label = m.getColumnLabel(i);
075: if (label == null)
076: label = "[NULL]";
077: int q = label.length();
078: if (q > maxcolwidth)
079: maxcolwidth = q;
080: }
081: maxcolwidth += 2;
082:
083: int numRows = 0;
084: long time = System.currentTimeMillis();
085: while (s.next()) {
086: ++numRows;
087: out
088: .println("---------------------------------------------------------");
089: for (int i = 1; i <= nCols; i++) {
090: String label = m.getColumnLabel(i);
091: if (label == null)
092: label = "[NULL]";
093: out.print(label);
094: for (int j = 0; j < (maxcolwidth - label.length()); j++)
095: out.print(" ");
096: try {
097: Object o;
098: // lookup what kind of column this is
099: if (m.getColumnType(i) == Types.LONGVARBINARY) {
100: o = s.getBytes(i);
101: //System.out.println(printByteArray((byte[])o));
102: } else {
103: o = s.getObject(i);
104: }
105:
106: if (o instanceof byte[]) {
107: if (showQuotes)
108: out.print("'");
109: out.print(new String((byte[]) o));
110: if (showQuotes)
111: out.print("'");
112: } else if ((o != null)
113: && (o.getClass().getName()
114: .equals("oracle.sql.CLOB"))) {
115: try {
116: Class oc = o.getClass();
117: Method method = oc.getMethod(
118: "getCharacterStream",
119: new Class[] {});
120: Reader r = (Reader) method.invoke(o,
121: new Object[] {});
122: char buffer[] = new char[1024];
123: int read = 0;
124: if (showQuotes)
125: out.print("'");
126: while ((read = r.read(buffer)) != -1) {
127: out.print(new String(buffer, 0, read));
128: }
129: if (showQuotes)
130: out.print("'");
131: } catch (Exception x) {
132: if (x instanceof SQLException)
133: throw (SQLException) x;
134:
135: System.out.println("");
136: System.out.println(" --> " + x.toString());
137: if (showQuotes)
138: out.print("'");
139: out.print(o.toString());
140: if (showQuotes)
141: out.print("'");
142: }
143: } else {
144: if (showQuotes)
145: out.print("'");
146: out.print(o.toString());
147: if (showQuotes)
148: out.print("'");
149: }
150: } catch (NullPointerException e) {
151: out.print("[NULL]");
152: if (showQuotes)
153: out.print("'");
154: }
155: out.println("");
156: }
157: }
158: time = System.currentTimeMillis() - time;
159: out.println("Command returned " + numRows + " rows");
160: out.println("Getting ResultSet contents took " + time + "ms");
161: }
162:
163: private static String printByteArray(byte[] b) {
164: StringBuffer sb = new StringBuffer();
165: for (int i = 0; i < b.length; i++) {
166: if ((i % 20) == 0)
167: sb.append("\n");
168: DecimalFormat format = new DecimalFormat("000");
169: sb.append(format.format((int) b[i]));
170: sb.append(" ");
171: }
172: return sb.toString();
173: }
174: }
|