Java Reference
In-Depth Information
ResultSetTableModel Method setQuery
Method setQuery (lines 144-163) executes the query it receives as an argument to obtain
a new ResultSet (line 152). Line 155 gets the ResultSetMetaData for the new Result-
Set . Line 158 uses ResultSet method last to position the ResultSet cursor at the last
row in the ResultSet . [ Note: This can be slow if the table contains many rows.] Line 159
uses ResultSet method getRow to obtain the row number for the current row in the Re-
sultSet . Line 162 invokes method fireTableStructureChanged (inherited from class
AbstractTableModel ) to notify any JTable using this ResultSetTableModel object as its
model that the structure of the model has changed. This causes the JTable to repopulate
its rows and columns with the new ResultSet data. Method setQuery throws any excep-
tions that occur in its body back to the application that invoked setQuery .
ResultSetTableModel Method disconnectFromDatabase
Method disconnectFromDatabase (lines 166-186) implements an appropriate termina-
tion method for class ResultSetTableModel . A class designer should provide a public
method that clients of the class must invoke explicitly to free resources that an object has
used. In this case, method disconnectFromDatabase closes the ResultSet , Statement
and Connection (lines 173-175), which are considered limited resources. Clients of the
ResultSetTableModel class should always invoke this method when the instance of this
class is no longer needed. Before releasing resources, line 168 verifies whether the connec-
tion is already terminated. If not, the method proceeds. The other methods in class Re-
sultSetTableModel each throw an IllegalStateException if connectedToDatabase is
false . Method disconnectFromDatabase sets connectedToDatabase to false (line 183)
to ensure that clients do not use an instance of ResultSetTableModel after that instance
has already been terminated. IllegalStateException is an exception from the Java librar-
ies that is appropriate for indicating this error condition.
DisplayQueryResults Class
Class DisplayQueryResults (Fig. 24.28) implements the application's GUI and interacts
with the ResultSetTableModel via a JTable object. This application also demonstrates
the JTable sorting and filtering capabilities.
1
// Fig. 24.28: DisplayQueryResults.java
2
// Display the contents of the Authors table in the topics database.
3
import java.awt.BorderLayout;
4
import java.awt.event.ActionListener;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.WindowAdapter;
7
import java.awt.event.WindowEvent;
8
import java.sql.SQLException;
9
import java.util.regex.PatternSyntaxException;
10
import javax.swing.JFrame;
11
import javax.swing.JTextArea;
12
import javax.swing.JScrollPane;
13
import javax.swing.ScrollPaneConstants;
14
15
import javax.swing.JTable;
import javax.swing.JOptionPane;
Fig. 24.28 | Display the contents of the Authors table in the books database. (Part 1 of 5.)
 
Search WWH ::




Custom Search