KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > jdbc > sl > ResultSetUtil


1 package com.protomatter.jdbc.sl;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.text.*;
54 import java.sql.*;
55 import java.io.*;
56 import java.lang.reflect.*;
57
58 /**
59  * A utility class for formatting JDBC ResultSets.
60  */

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 JavaDoc 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 JavaDoc 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 JavaDoc o;
102           // lookup what kind of column this is
103
if (m.getColumnType(i) == Types.LONGVARBINARY)
104           {
105             o = s.getBytes(i);
106             //System.out.println(printByteArray((byte[])o));
107
}
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 JavaDoc((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 JavaDoc oc = o.getClass();
124               Method method = oc.getMethod("getCharacterStream", new Class JavaDoc[] { });
125               Reader r = (Reader)method.invoke(o, new Object JavaDoc[] { });
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 JavaDoc(buffer, 0, read));
132               }
133               if (showQuotes) out.print("'");
134             }
135             catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc printByteArray(byte[] b)
168   {
169     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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