The Statement Class
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';
Statement class has two subtypes:
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
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 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.
- Note: You can still use a
CallableStatementobject if you do not use the
Creating a Statement Object
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
java.sql.Statement statement = connection.createStatement();