KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > ResultOutputUtil


1 /**
2  * com.mckoi.util.ResultOutputUtil 22 Sep 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.sql.*;
28 import java.io.*;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * Utilities for parsing a ResultSet and outputing it in different forms. The
33  * forms included are straight text (mono-spaced), HTML, etc.
34  *
35  * @author Tobias Downer
36  */

37
38 public class ResultOutputUtil {
39
40
41   /**
42    * Writes a break.
43    * eg. "+--------+----------+---------------+"
44    */

45   private static void writeBreak(int[] widths, PrintWriter out) {
46     out.print('+');
47     for (int i = 0; i < widths.length; ++i) {
48       int wid = widths[i] + 2;
49       for (int n = 0; n < wid; ++n) {
50         out.print('-');
51       }
52       out.print('+');
53     }
54     out.println();
55   }
56
57   /**
58    * Writes a row of data.
59    * eg. "|1 |Greetings |Part-54445 |"
60    */

61   private static void writeRow(int[] widths, String JavaDoc[] cols, PrintWriter out) {
62     out.print('|');
63     for (int i = 0; i < widths.length; ++i) {
64       String JavaDoc str = cols[i];
65       out.print(' ');
66       out.print(str);
67       // Write padding
68
int wid = (widths[i] + 1) - str.length();
69       for (int n = 0; n < wid; ++n) {
70         out.print(' ');
71       }
72       out.print('|');
73     }
74     out.println();
75   }
76
77   /**
78    * Formats the ResultSet as plain mono-spaced text and outputs the result to
79    * the PrintWriter.
80    */

81   public static void formatAsText(ResultSet result_set, PrintWriter out)
82                                                          throws SQLException {
83     ResultSetMetaData meta_data = result_set.getMetaData();
84     // Maximum widths of each column.
85
int[] max_widths = new int[meta_data.getColumnCount()];
86     Vector JavaDoc[] data = new Vector JavaDoc[meta_data.getColumnCount()];
87     for (int i = 0; i < data.length; ++i) {
88       data[i] = new Vector JavaDoc();
89     }
90     int row_count = 0;
91
92     for (int i = 0; i < data.length; ++i) {
93       String JavaDoc str = meta_data.getColumnLabel(i + 1);
94       max_widths[i] = Math.max(str.length(), max_widths[i]);
95     }
96
97     // Read in the data for the result set,
98
while (result_set.next()) {
99       for (int i = 0; i < data.length; ++i) {
100         Object JavaDoc ob = result_set.getObject(i + 1);
101         String JavaDoc str = "NULL";
102         if (ob != null) {
103           str = ob.toString();
104         }
105         data[i].addElement(str);
106         max_widths[i] = Math.max(str.length(), max_widths[i]);
107       }
108       ++row_count;
109     }
110
111     // Output the data we stored
112
String JavaDoc[] line = new String JavaDoc[data.length];
113
114     writeBreak(max_widths, out);
115     for (int n = 0; n < line.length; ++n) {
116       line[n] = meta_data.getColumnLabel(n + 1);
117     }
118     writeRow(max_widths, line, out);
119     writeBreak(max_widths, out);
120     for (int i = 0; i < row_count; ++i) {
121       for (int n = 0; n < line.length; ++n) {
122         line[n] = (String JavaDoc) data[n].elementAt(i);
123       }
124       writeRow(max_widths, line, out);
125     }
126     writeBreak(max_widths, out);
127
128   }
129
130
131 }
132
Popular Tags