JDBC Statement
Jakob Jenkov |
The Java JDBC Statement, java.sql.Statement, interface is used to execute SQL statements
against a relational database. You obtain a JDBC Statement from a
JDBC Connection. Once you have a Java Statement instance you can execute
either a database query or an database update with it.
This Java JDBC Statement tutorial will explain how you use a Statement to execute
queries, updates, and how to properly close the Statement instance when you are done with it.
Create Statement
In order to use a Java JDBC Statement you first need to create a Statement.
Here is an example of creating a Java Statement instance:
Statement statement = connection.createStatement();
The connection instance is a Java JDBC Connection instance.
Executing a Query via a Statement
Once you have created a Java Statement object, you can execute a query against the database.
You do so by calling its executeQuery() method, passing an SQL statement as parameter.
The Statement executeQuery() method returns a Java JDBC ResultSet
which can be used to navigate the response of the query. Here is an example of calling the
Java JDBC Statement executeQuery() and navigating the returned ResultSet:
String sql = "select * from people";
ResultSet result = statement.executeQuery(sql);
while(result.next()) {
String name = result.getString("name");
long age = result.getLong ("age");
}
Remember that the ResultSet needs to be closed when you are done with it.
Execute an Update via a Statement
You can also execute an update of the database via a Java JDBC Statement instance. For instance,
you could execute an SQL insert, update or delete via a Statement instance. Here is an
example of executing a database update via a Java JDBC Statement instance:
Statement statement = connection.createStatement(); String sql = "update people set name='John' where id=123"; int rowsAffected = statement.executeUpdate(sql);
The rowsAffected returned by the statement.executeUpdate(sql) call, tells
how many records in the database were affected by the SQL statement.
Closing a Statement
Once you are finished with a Statement instance you need to close it. You close a
Statement instance by calling its close() method. Here is an example
of closing a Java JDBC Statement instance:
statement.close();
Closing a Statement Using Java Try With Resources
In order to close a Statement correctly after use, you can open it inside a
Java Try With Resources block. Here is an example
of closing a Java JDBC Statement instance using the try-with-resources construct:
try(Statement statement = connection.createStatement()) {
//use the statement in here.
} catch(SQLException e) {
}
Once the try block exits, the Statement will be closed automatically.
Statement vs. PreparedStatement
The Java JDBC API has an interface similar to the Statement called
PreparedStatement . The PreparedStatement can have parameters
inserted into the SQL statement, so the PreparedStatement can be reused again and again with
different parameter values. You cannot do that with a Statement. A Statement requires
a finished SQL statement as parameter.
| Tweet | |
Jakob Jenkov | |











