public class ResultSetTableModel extends AbstractTableModel
1: // Fig. 25.28: ResultSetTableModel.java
2: // A TableModel that supplies ResultSet data to a JTable.
3: import java.sql.Connection;
4: import java.sql.Statement;
5: import java.sql.DriverManager;
6: import java.sql.ResultSet;
7: import java.sql.ResultSetMetaData;
8: import java.sql.SQLException;
9: import javax.swing.table.AbstractTableModel;
10:
11: // ResultSet rows and columns are counted from 1 and JTable
12: // rows and columns are counted from 0. When processing
13: // ResultSet rows or columns for use in a JTable, it is
14: // necessary to add 1 to the row or column number to manipulate
15: // the appropriate ResultSet column (i.e., JTable column 0 is
16: // ResultSet column 1 and JTable row 0 is ResultSet row 1).
17: public class ResultSetTableModel extends AbstractTableModel
18: {
19: private Connection connection;
20: private Statement statement;
21: private ResultSet resultSet;
22: private ResultSetMetaData metaData;
23: private int numberOfRows;
24:
25: // keep track of database connection status
26: private boolean connectedToDatabase = false;
27:
28: // constructor initializes resultSet and obtains its meta data object;
29: // determines number of rows
30: public ResultSetTableModel( String driver, String url,
31: String username, String password, String query )
32: throws SQLException, ClassNotFoundException
33: {
34: // load database driver class
35: Class.forName( driver );
36:
37: // connect to database
38: connection = DriverManager.getConnection( url, username, password );
39:
40: // create Statement to query database
41: statement = connection.createStatement(
42: ResultSet.TYPE_SCROLL_INSENSITIVE,
43: ResultSet.CONCUR_READ_ONLY );
44:
45: // update database connection status
46: connectedToDatabase = true;
47:
48: // set query and execute it
49: setQuery( query );
50: } // end constructor ResultSetTableModel
51:
52: // get class that represents column type
53: public Class getColumnClass( int column ) throws IllegalStateException
54: {
55: // ensure database connection is available
56: if ( !connectedToDatabase )
57: throw new IllegalStateException( "Not Connected to Database" );
58:
59: // determine Java class of column
60: try
61: {
62: String className = metaData.getColumnClassName( column + 1 );
63:
64: // return Class object that represents className
65: return Class.forName( className );
66: } // end try
67: catch ( Exception exception )
68: {
69: exception.printStackTrace();
70: } // end catch
71:
72: return Object.class; // if problems occur above, assume type Object
73: } // end method getColumnClass
74:
75: // get number of columns in ResultSet
76: public int getColumnCount() throws IllegalStateException
77: {
78: // ensure database connection is available
79: if ( !connectedToDatabase )
80: throw new IllegalStateException( "Not Connected to Database" );
81:
82: // determine number of columns
83: try
84: {
85: return metaData.getColumnCount();
86: } // end try
87: catch ( SQLException sqlException )
88: {
89: sqlException.printStackTrace();
90: } // end catch
91:
92: return 0; // if problems occur above, return 0 for number of columns
93: } // end method getColumnCount
94:
95: // get name of a particular column in ResultSet
96: public String getColumnName( int column ) throws IllegalStateException
97: {
98: // ensure database connection is available
99: if ( !connectedToDatabase )
100: throw new IllegalStateException( "Not Connected to Database" );
101:
102: // determine column name
103: try
104: {
105: return metaData.getColumnName( column + 1 );
106: } // end try
107: catch ( SQLException sqlException )
108: {
109: sqlException.printStackTrace();
110: } // end catch
111:
112: return ""; // if problems, return empty string for column name
113: } // end method getColumnName
114:
115: // return number of rows in ResultSet
116: public int getRowCount() throws IllegalStateException
117: {
118: // ensure database connection is available
119: if ( !connectedToDatabase )
120: throw new IllegalStateException( "Not Connected to Database" );
121:
122: return numberOfRows;
123: } // end method getRowCount
124:
125: // obtain value in particular row and column
126: public Object getValueAt( int row, int column )
127: throws IllegalStateException
128: {
129: // ensure database connection is available
130: if ( !connectedToDatabase )
131: throw new IllegalStateException( "Not Connected to Database" );
132:
133: // obtain a value at specified ResultSet row and column
134: try
135: {
136: resultSet.absolute( row + 1 );
137: return resultSet.getObject( column + 1 );
138: } // end try
139: catch ( SQLException sqlException )
140: {
141: sqlException.printStackTrace();
142: } // end catch
143:
144: return ""; // if problems, return empty string object
145: } // end method getValueAt
146:
147: // set new database query string
148: public void setQuery( String query )
149: throws SQLException, IllegalStateException
150: {
151: // ensure database connection is available
152: if ( !connectedToDatabase )
153: throw new IllegalStateException( "Not Connected to Database" );
154:
155: // specify query and execute it
156: resultSet = statement.executeQuery( query );
157:
158: // obtain meta data for ResultSet
159: metaData = resultSet.getMetaData();
160:
161: // determine number of rows in ResultSet
162: resultSet.last(); // move to last row
163: numberOfRows = resultSet.getRow(); // get row number
164:
165: // notify JTable that model has changed
166: fireTableStructureChanged();
167: } // end method setQuery
168:
169: // close Statement and Connection
170: public void disconnectFromDatabase()
171: {
172: if ( !connectedToDatabase )
173: return;
174:
175: // close Statement and Connection
176: try
177: {
178: statement.close();
179: connection.close();
180: } // end try
181: catch ( SQLException sqlException )
182: {
183: sqlException.printStackTrace();
184: } // end catch
185: finally // update database connection status
186: {
187: connectedToDatabase = false;
188: } // end finally
189: } // end method disconnectFromDatabase
190: } // end class ResultSetTableModel
191:
192:
193:
194:
195: /**************************************************************************
196: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
197: * Pearson Education, Inc. All Rights Reserved. *
198: * *
199: * DISCLAIMER: The authors and publisher of this book have used their *
200: * best efforts in preparing the book. These efforts include the *
201: * development, research, and testing of the theories and programs *
202: * to determine their effectiveness. The authors and publisher make *
203: * no warranty of any kind, expressed or implied, with regard to these *
204: * programs or to the documentation contained in these books. The authors *
205: * and publisher shall not be liable in any event for incidental or *
206: * consequential damages in connection with, or arising out of, the *
207: * furnishing, performance, or use of these programs. *
208: *************************************************************************/