Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Overpower the PreparedStatement

Save time debugging your database queries with DebuggableStatement

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Shortly after you gain database coding experience, you discover the benefits of the java.sql package's PreparedStatement, which delivers a performance boost by reusing database calls, thus more efficiently interacting with the database. Armed with that knowledge, you happily employ PreparedStatement, only to discover that your first debugging session has become more difficult. You know your PreparedStatement's SQL command as well as the statement's parameter values ... or do you? Enter the DebuggableStatement. In this article, we demonstrate how a DebuggableStatement can provide the answers necessary to avoid a potential debugging nightmare.

Note: To download this article's source code, see Resources.

The prepared statement

How an application interacts with the database represents an often-overlooked aspect of application development. Indeed, many programmers find the simplest way to get the data, then leave the database-tuning to the database administrator. However, application-side Java techniques can boost your database's performance, thus improving your application's efficiency. As mentioned above, Java's PreparedStatement interface frequently offers the best way to accomplish that task.

First, consider some background by asking, "Why use a PreparedStatement?" Indeed, you can accomplish your SQL tasks with the Statement class. However, Statement causes problems. Your SQL statements can fall into two categories: static or dynamic. With a static query, the string doesn't change; you create it once, then call it over and over again. In contrast, with a dynamic query, you create a new SQL command by concatenating strings and variables, then instantiating a new Statement object and executing that command. Either way, each time you execute a new command, the database must compile the SQL code. Such compilation can include costly syntax checking, name validation, and pseudo code generation. If you never worry about your database's performance, read no further and live your life in bliss.

If, however, you do worry about your database's performance, you may consider PreparedStatement. A PreparedStatement is the Java encapsulation of a parameterized query in which the SQL statement compiles a single time, but can execute many times. To change the query conditions, you employ placeholders within the statement to indicate bind variables. After such bind variables (parameters to the query) are set, the query executes, (note the question mark indicating a bind variable in the following example's query):

try {
  Connection con = DriverManager.getConnection(url);
  String select = "SELECT * FROM customer_tbl WHERE customer_id = ?";
  PreparedStatement ps = con.prepareStatement(select);
  ResultSet rs = null;
  for(int i=0;i<10;i++) {
//  PreparedStatement Example
    ps.setString(1,""+i);
    rs = ps.executeQuery();
  }
} catch (SQLException sqle) {sqle.printStackTrace();}


In the example, the code executes 10 queries, but, rather than compiling and validating each new SQL string built with a different customer ID, then executing the query, the database sees one query with a bind variable. The database uses the query over and over again, thus avoiding costly compilation and potentially delivering a tremendous performance boost.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (4)
Login
Forgot your account info?

how to implement the source code available in my applicationBy Anonymous on December 30, 2009, 7:34 amhow to implement the source code available in my application because when i tried to run it gives me an error saying: "DebuggableStatement.java": DebuggableStatement...

Reply | Read entire comment

can i expect the following output from the DebuggableStatement uBy Anonymous on December 21, 2009, 7:31 amfor this System.out.println(" debuggable statement= " + ps.toString()); can i expect the following output: debuggable statement= SELECT name,rate FROM EmployeeTable...

Reply | Read entire comment

Prepared Statement DebuggingBy Anonymous on April 24, 2009, 11:15 amCan you please explain how did you implement those methods ? I imported the source code from this article to try this out and first thing I found was executeUpdate()...

Reply | Read entire comment

thanks a lotBy Anonymous on March 6, 2009, 5:31 pmI found your teachings really useful,although I had to override a lot of more methods in your DebuggableStatement class to make all work I'm using java 1.5

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources