Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Java Q&A


May01: Making a Statement with JDBC

Making a Statement with JDBC

JDBC provides three methods for executing SQL statements: normal statements, prepared statements, and callable statements. The first method is to obtain a Statement interface handle by calling dbConnection.createStatement(). Then a ResultSet can be obtained using ResultSet resultSet = statement.executeQuery(''select * from member");. The executeQuery() method is used for, big surprise, queries; while the executeUpdate() method is used to execute insert, update, and delete statements. The Java documentation calls this "static SQL" due to the hardcoded nature of the SQL statement. Others often refer to this as dynamic SQL because the query must be dynamically created by the database instead of being precompiled.

Precompiled statements are known as prepared statements in JDBC. They are more efficient than regular statements if you are executing similar statements multiple times. Prepared statements can take up to 70 percent of the time required by raw queries (see "JDBC and Database Connection Pooling" in Marty Hall's book Core Servlets and Java Server Pages). When a PreparedStatement is created, the SQL statement is sent to the database for precompilation. The statement declaration includes query parameters denoted by the question mark symbol. Methods such as setString are provided to set the parameters like this:

PreparedStatement statement =

dbConnection.prepareStatement("select * from member

where firstname = ?");

Statement.setString( 1, "John");

The same parameters will remain in force for repeated use of a statement until clearParameters() is called. Currently, the precompiled form of a prepared statement is cached only as long as the PreparedStatement instance is alive, so the performance benefits are only obtained when reusing the same PreparedStatement instance, setting different parameters on each invocation. However, the current proposal for JDBC 3.0 specifies pooling prepared statements transparently. No syntax changes would be necessary, according to the proposal, in order to have prepared statements seamlessly cached across instances of PreparedStatement lifetimes.

The other type of statement available in JDBC is a callable statement. It's used to execute SQL stored procedures and allows them to be called in the same way for all RDBMSs. Stored procedures can have a variable number of parameters used for input (IN parameters), output (OUT parameters), or both (INOUT parameters). Like prepared statements, callable statements also use a question mark as a placeholder for a parameter. The CallableStatement interface extends PreparedStatement, which extends the Statement interface. CallableStatement has methods dealing with OUT and INOUT parameters. Methods inherited from PreparedStatement are used for setting IN parameters. Procedures that return multiple result sets are handled with methods inherited from Statement. For procedures with OUT or INOUT parameters, the type of the parameter must be registered before executing the statement. A basic stored procedure invocation will resemble the following:

CallableStatement statement = dbCon- nection.prepareCall

("{? = call getDepartmentID(?)}");

statement.registerOutParameter(1, java.sql.Types.INTEGER);

statement.setInt(2, employeeID);

ResultSet resultSet = statement.execute- Query();

int departmentID = statement.getInt(1);

— T.S.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.