2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* ResultSetHelper.java
19
* Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
26
import java.sql.ResultSet;
27
import java.sql.ResultSetMetaData;
28
import java.sql.Types;
31
* Represents an extended JTable, containing a table model based on a ResultSet
32
* and the corresponding query.
35
* @author FracPete (fracpete at waikato dot ac dot nz)
36
* @version $Revision: 1.2 $
39
public class ResultSetHelper {
40
/** the resultset to work on */
41
protected ResultSet m_ResultSet;
43
/** whether we initialized */
44
protected boolean m_Initialized = false;
46
/** the maximum number of rows to retrieve */
47
protected int m_MaxRows = 0;
49
/** the number of columns */
50
protected int m_ColumnCount = 0;
52
/** the number of rows */
53
protected int m_RowCount = 0;
55
/** the column names */
56
protected String[] m_ColumnNames = null;
58
/** whether a column is numeric */
59
protected boolean[] m_NumericColumns = null;
61
/** the class for each column */
62
protected Class[] m_ColumnClasses = null;
65
* initializes the helper, with unlimited number of rows
66
* @param rs the resultset to work on
68
public ResultSetHelper(ResultSet rs) {
73
* initializes the helper, with the given maximum number of rows (less than
75
* @param rs the resultset to work on
76
* @param max the maximum number of rows to retrieve
78
public ResultSetHelper(ResultSet rs, int max) {
86
* initializes, i.e. reads the data, etc.
88
protected void initialize() {
89
ResultSetMetaData meta;
96
meta = m_ResultSet.getMetaData();
99
m_ColumnNames = new String[meta.getColumnCount()];
100
for (i = 1; i <= meta.getColumnCount(); i++)
101
m_ColumnNames[i - 1] = meta.getColumnName(i);
104
m_NumericColumns = new boolean[meta.getColumnCount()];
105
for (i = 1; i <= meta.getColumnCount(); i++)
106
m_NumericColumns[i - 1] = typeIsNumeric(meta.getColumnType(i));
109
m_ColumnClasses = new Class[meta.getColumnCount()];
110
for (i = 1; i <= meta.getColumnCount(); i++) {
112
m_ColumnClasses[i - 1] = Class.forName(meta.getColumnClassName(i));
114
catch (Exception e) {
115
//e.printStackTrace();
116
// JDBC does not support this function -> do it manually
118
m_ColumnClasses[i - 1] = typeToClass(meta.getColumnType(i));
120
catch (Exception ex) {
121
m_ColumnClasses[i - 1] = String.class;
127
m_ColumnCount = meta.getColumnCount();
133
m_ResultSet.absolute(m_MaxRows);
134
m_RowCount = m_ResultSet.getRow();
136
catch (Exception ex) {
142
m_RowCount = m_ResultSet.getRow();
145
// sometimes, e.g. with a "desc <table>", we can't use absolute(int)
148
if ( (m_RowCount == 0) && (m_ResultSet.first()) ) {
150
while (m_ResultSet.next()) {
152
if (m_ResultSet.getRow() == m_MaxRows)
157
catch (Exception e) {
161
m_Initialized = true;
163
catch (Exception ex) {
169
* the underlying resultset
171
public ResultSet getResultSet() {
176
* returns the number of columns in the resultset
178
public int getColumnCount() {
181
return m_ColumnCount;
185
* returns the number of rows in the resultset
187
public int getRowCount() {
194
* returns an array with the names of the columns in the resultset
196
public String[] getColumnNames() {
199
return m_ColumnNames;
203
* returns an array that indicates whether a column is numeric or nor
205
public boolean[] getNumericColumns() {
208
return m_NumericColumns;
212
* returns the classes for the columns
214
public Class[] getColumnClasses() {
217
return m_ColumnClasses;
221
* whether a limit on the rows to retrieve was set
223
public boolean hasMaxRows() {
224
return (m_MaxRows > 0);
228
* the maximum number of rows to retrieve, less than 1 means unlimited
230
public int getMaxRows() {
235
* returns an 2-dimensional array with the content of the resultset, the first
236
* dimension is the row, the second the column (i.e., getCells()[y][x]).
237
* Note: the data is not cached! It is always retrieved anew.
239
public Object[][] getCells() {
246
result = new Object[getRowCount()][getColumnCount()];
251
for (i = 0; i < getRowCount(); i++) {
253
for (n = 0; n < getColumnCount(); n++) {
255
result[i][n] = m_ResultSet.getObject(n + 1);
257
catch (Exception e) {
263
if (i == getRowCount() - 1)
269
catch (Exception e) {
277
* Returns the class associated with a SQL type.
279
* @param type the SQL type
280
* @return the Java class corresponding with the type
282
public static Class typeToClass(int type) {
290
result = String.class;
292
result = Boolean.class;
295
result = Character.class;
298
result = java.sql.Date.class;
301
result = Double.class;
304
result = Double.class;
307
result = Float.class;
310
result = Integer.class;
312
case Types.LONGVARBINARY:
313
result = String.class;
315
case Types.LONGVARCHAR:
316
result = String.class;
319
result = String.class;
322
result = Double.class;
325
result = String.class;
328
result = Double.class;
331
result = Short.class;
334
result = java.sql.Time.class;
336
case Types.TIMESTAMP:
337
result = java.sql.Timestamp.class;
340
result = Short.class;
342
case Types.VARBINARY:
343
result = String.class;
346
result = String.class;
356
* returns whether the SQL type is numeric (and therefore the justification
358
* @param type the SQL type
359
* @return whether the given type is numeric
361
public static boolean typeIsNumeric(int type) {
391
case Types.LONGVARBINARY:
394
case Types.LONGVARCHAR:
415
case Types.TIMESTAMP:
421
case Types.VARBINARY: