Java Reference
In-Depth Information
"Execute Query" button. Otherwise, this class is simpler than its counterparts in preceding chapters as
you no longer need a
parseTable()
method or a
TableChangeListener
.
The
JTable
is preloaded using the SQL query as shown in the following:
"SELECT TOP 5 * FROM " + tableName;
You use the
TOP 5
limitation to prevent having to load a huge
JTable
in cases where the database
table is large. Obviously, you can change this to suit your own application.
The
TableQueryFrame
class is different from its counterparts in previous chapters primarily because
it is driven by the
JTextArea
rather than by the
JTable
. The
JTextArea
is used to enter free form
SQL queries for execution when the "Execute Query" button is clicked.
The sequence of events involved in using the
TableQueryFrame
(shown in
Listing 7-3
) example is as
follows:
1. User selects a database.
2. User selects "View ResultSet".
3. User selects the table.
4. A
TableQueryFrame
is displayed showing the top five records from the table.
5. A SQL command is typed into the
JTextArea
and executed on command.
This example extends the examples of
Chapters 5
and
6
to create a Swing-based application that can
connect to any database-management system. This example can be used to create and populate
tables, and to execute any of the queries discussed in this chapter. The TableQueryFrame code is
shown in
Listing 7-3
.
Listing 7-3: TableQueryFrame
package jdbc_bible.part2;
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import java.util.EventListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
/**
* TableQueryFrame extends JInternalFrame to create a display which builds SQL