001: package com.pk;
002:
003: import java.sql.*;
004: import javax.swing.table.*;
005: import java.util.Vector;
006: import javax.swing.*;
007:
008: class ResultsModel extends AbstractTableModel {
009:
010: /**
011: *
012: */
013: private static final long serialVersionUID = 6707027182873294943L;
014: String[] columnNames = new String[0];
015: Vector dataRows; // Empty vector of rows
016:
017: ResultSet gResults;
018: int gColumns;
019: String[] gRowData;
020: int arraySize = 100;
021: int totalRowCount = 0;
022:
023: public void setResultSet(ResultSet results) {
024: try {
025: if (results == null) {
026: gColumns = 0;
027: totalRowCount = 0;
028: columnNames = new String[0];
029: fireTableChanged(null);
030: return;
031: }
032: gResults = results;
033:
034: ResultSetMetaData metadata = results.getMetaData();
035:
036: int columns = metadata.getColumnCount(); // Get number of columns
037:
038: gColumns = columns;
039:
040: columnNames = new String[columns]; // Array to hold names
041: totalRowCount = 0;
042:
043: // Get the column names
044: for (int i = 0; i < columns; i++)
045: columnNames[i] = metadata.getColumnLabel(i + 1);
046:
047: // Get all rows.
048: dataRows = new Vector(); // New Vector to store the data
049: String[] rowData; // Stores one row
050: while (results.next()) // For each row...
051: {
052: rowData = new String[columns]; // create array to hold the data
053: for (int i = 0; i < columns; i++)
054: // For each column
055: rowData[i] = results.getString(i + 1); // retrieve the data item
056:
057: dataRows.addElement(rowData); // Store the row in the vector
058: totalRowCount++;
059: if (results.getRow() == arraySize)
060: break;
061: }
062:
063: fireTableChanged(null); // Signal the table there is new model data
064: } catch (SQLException sqle) {
065: System.err.println(sqle);
066: }
067:
068: }
069:
070: public void setNextResultSet(JTable table) {
071: try {
072: int lastRow = gResults.getRow() + arraySize;
073: int[] colWidth = new int[gColumns];
074:
075: while (gResults.next()) // For each row...
076: {
077: gRowData = new String[gColumns]; // create array to hold the data
078: for (int i = 0; i < gColumns; i++)
079: // For each column
080: gRowData[i] = gResults.getString(i + 1); // retrieve the data item
081:
082: dataRows.addElement(gRowData); // Store the row in the vector
083: totalRowCount++;
084: if (gResults.getRow() == lastRow)
085: break;
086: }
087:
088: for (int i = 0; i < gColumns; i++) {
089: colWidth[i] = table.getColumnModel().getColumn(i)
090: .getWidth();
091: }
092:
093: fireTableChanged(null);
094:
095: for (int i = 0; i < gColumns; i++) {
096: table.getColumnModel().getColumn(i).setWidth(
097: colWidth[i]);
098: }
099: } catch (SQLException sqle) {
100: System.err.println(sqle);
101: }
102:
103: }
104:
105: public int getColumnCount() {
106: return columnNames.length;
107: }
108:
109: public int getRowCount() {
110: // if(dataRows == null)
111: // return 0;
112: //else
113: //return dataRows.size();
114: return totalRowCount;
115: }
116:
117: public Object getValueAt(int row, int column) {
118: return ((String[]) (dataRows.elementAt(row)))[column];
119: }
120:
121: public String getColumnName(int column) {
122: return columnNames[column] == null ? "No Name"
123: : columnNames[column];
124: }
125:
126: }
|