Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put the prepared statement OBJECTS in Java?

I want to use prepared statements. I have read that the advantage of prepared statements is that they don't have to be parsed/compiled every time again so one reduces load. Now my question is, where the "recognition" of an already prepared statement takes place, in Java or within my DB system? I ask, because I want to know where to store my PreparedStatement object in my code: as class attribute and set the parameters with every request OR create a new prepared statement object whenever there is a request.

    public class Option1 {
       private PreparedStatement myStatement;
       public Option1() {
          // create the myStatement object
          myStatement = conn.prepareStatement("");
       }
       public List<Items> query() {
          // just use the myStatement object
          myStatement.setString(1, "foo");
       }
   }

    public class Option2 {
       public List<Items> query() {
          PreparedStatement myLocalStatement = conn.prepareStatement("");;
          // create and use the statement
          myLocalStatement.setString(1, "foo");
       }
   }

Now my question is, what is the better way to do it, option 1 or 2? However, I have to do a "cleanup" after every execution by doing a myStatement.close() right?

Maybe I should ask it in another way: how to reuse it in the most effective way?

UPDATE: in the case there are two answers, one prefering option 1 and one option 2, I would kindly ask the community to vote for their choice ^^

like image 701
strauberry Avatar asked Sep 03 '12 08:09

strauberry


People also ask

What is PreparedStatement in Java JDBC?

public interface PreparedStatement extends Statement. An object that represents a precompiled SQL statement. A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

Is PreparedStatement an interface in Java?

PreparedStatement interface. The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized query.

Do we need to close PreparedStatement in Java?

Closing PreparedStatement Object A simple call to the close() method will do the job. If you close the Connection object first, it will close the PreparedStatement object as well. However, you should always explicitly close the PreparedStatement object to ensure proper cleanup.

Can we create a table with PreparedStatement?

A JDBC PreparedStatement example to create a table in the database. A table 'employee' is created.

How do you execute a PreparedStatement?

Executing the Prepared Statement Once you have created the PreparedStatement object you can execute it using one of the execute() methods of the PreparedStatement interface namely, execute(), executeUpdate() and, executeQuery().


1 Answers

SQL databases are caching the execution plan for statement with the complete query (including where clause) as the key. By using prepared statements, the query is the same, no matter what values you are using (they are always '?').

So from the database cache perspective there is no difference between your two options. See this article, that also describes some Java EE specific issues.

But of course there are some other factors from the code perspective, as others mentioned, especially when you are reusing it very often (like in this example). On important thing ist that some JDBC drivers are supporting precompilation.
Keep in mind that prepared statements are initially overheading while catching up with every subsequent use - here's a great chapter about that.

like image 113
Zeemee Avatar answered Oct 09 '22 15:10

Zeemee