The Statement Class
Go Up to Executing SQL Statements in InterClient Programs
The java.sql.Statement
interface allows you to execute a static SQL statement and to retrieve the results produced by the query. You cannot change any values with a static statement. For example, the following SQL statement displays information once for specific employees:
SELECT first_name, last_name, dept_name FROM emp_table WHERE dept_name = 'pubs';
The Statement
class has two subtypes: PreparedStatement
and CallableStatement
.
PreparedStatement
The PreparedStatement
object allows you to execute a set of SQL statements more than once. Instead of creating and parsing a new statement each time to do the same function, you can use the PreparedStatement
class to execute pre-compiled SQL statements multiple times. This class has a series of “setXXX” methods that allow your code to pass parameters to a predefined SQL statement; it is like a template to which you supply the parameters. Once you have defined parameter values for a statement, they remain to be used in subsequent executions until you clear them with a call to the PreparedStatement.clearParameters
method.
For example, suppose you want to be able to print a list of all new employees hired on any given day. The operator types in the date, which is then passed into the PreparedStatement
object. Only those employees or rows in “emp_table” where “hire_date” matches the input date are returned in the result set.
SELECT first_name, last_name, emp_no FROM emp_table WHERE hire_date = ?;
See Selecting Data with PreparedStatement for more on how this construct works.
CallableStatement
The CallableStatement
class is used for executing stored procedures with OUT
parameters. Since InterBase does not support the use of OUT
parameters, there is no need to use CallableStatement
with InterClient.
You can still use a
CallableStatement
object if you do not use the OUT
parameter methods.Creating a Statement Object
Creating a Statement
object allows you to execute an SQL query, assuming that you have already created the connection object. The example below shows how to use the createStatement
method to create a Statement
object:
java.sql.Statement statement = connection.createStatement();