An SQL
SELECT statement is used to query a table. The table to query is determined by parsing the
SELECT string when creating the
SELECT statement. The rows which satisfy the query are returned in a
ResultSet. A
WHERE clause can be used in the
SELECT statement to control which rows of a table should be returned. An
ORDER BY
clause can be optionally appended after the
WHERE clause to sort the resulting rows of the query. A
LIMIT clause can be optionally appended as the last clause of the
SELECT string to control the number of rows ultimately returned for the query.
Procedure
Formulate an SQL
SELECT for the query you want to use to retrieve rows from a table.
Call the create statement method of the session object.
Supply the SQL
SELECT string as an argument.
Call the statement methods to set the parameter values for the query.
Run the statement by calling
executeQuery() method for the SQL statement.
Read the rows of the query
ResultSet by looping and calling the following methods until no more rows are available to read:
Call the
hasNext() method of the
ResultSet to see if there is a row to read.
Call the
next() method of the
ResultSet to read the next row.
When all of the rows have been read, close the
ResultSet object.
Optional: Set different parameter values and rerun the statement by repeating
Step 3 -
Step 5.