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 ^^
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.
PreparedStatement interface. The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized query.
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.
A JDBC PreparedStatement example to create a table in the database. A table 'employee' is created.
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().
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With