The SQL SELECT Statement

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

  1. Formulate an SQL SELECT for the query you want to use to retrieve rows from a table.
  2. Call the create statement method of the session object.
    Supply the SQL SELECT string as an argument.
  3. Call the statement methods to set the parameter values for the query.
  4. Run the statement by calling executeQuery() method for the SQL statement.
  5. Read the rows of the query ResultSet by looping and calling the following methods until no more rows are available to read:
    1. Call the hasNext() method of the ResultSet to see if there is a row to read.
    2. Call the next() method of the ResultSet to read the next row.
    3. When all of the rows have been read, close the ResultSet object.
  6. Optional: Set different parameter values and rerun the statement by repeating Step 3 - Step 5.
  7. Close the statement object.
Related tasks