Issuing a query and processing the results
Queries can be executed using two different kinds of statements. Simple queries can be executed using static statements, while prepared statements allow the changing of parameters before execution.
Static statements
To execute a static SQL statement, a Statement
object has to be obtained from the Connection
object first:
Statement statement = connection.createStatement();
The Statement
can then be used to execute queries or updates on the target database.
As with the connection, it is good practice to close a statement as soon as it stops being used. If the statement is not closed, it will be closed implicitly when the underlying connection is closed.
The following example shows the way to get data from a table using the executeQuery
method:
ResultSet resultSet = statement.executeQuery( "SELECT account_id, first_name, last_name FROM account");
The method returns a ResultSet
, which will be introduced later in this chapter.
To insert, update...