Java Reference
In-Depth Information
executes, it sets the database to the initial state. While it is expected that this
program is executed only once to establish the database initially, it is possible to
perform additional executions, deleting any subsequent additions or changes made
to the database within these three tables. To verify that the Users table contains
only the expected data and that the other tables do not contain any records, a
query is executed on each table.
Creating and Executing SQL Database Queries
The executeQuery() method of either a Statement or PreparedStatement
object is called to perform an SQL query. Typically, an SQL SELECT statement is
executed when performing a query. Values of the specified columns are returned
for all records matching the search condition in the WHERE clause. Results of a
query are returned as a ResultSet object , which is a table of data representing
the database result set. By default, only one ResultSet object per Statement object
can be opened at the same time. A ResultSet object is closed automatically when
the Statement is re-executed or closed. The same is true for a PreparedStatement,
because it also is a Statement object, as it inherits from Statement.
The asterisk in line 166 indicates that all columns or fields of the Users table
are to be returned. Because no search conditions are set, the result set also will
include all records. The following step enters code to create and execute an SQL
Query Statement.
To Execute an SQL Query Statement
1. Enter lines 165 and 166 as shown in Figure 11-17 on page 711.
The executeQuery() method of the Statement object returns a ResultSet
object containing the results of the query (Figure 11-30).
executeQuery()
method
ResultSet
static
Statement
FIGURE 11-30
 
Search WWH ::




Custom Search