views:

1445

answers:

4

I would like to know if we can reuse the same Statement object for executing more than one query. Or, should we create a new statement for different queries.

For example,

Connection con = getDBConnection();
Statement st1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
int i = st1.executeUpdate("update tbl_domu set domU_status=1 where domU_id=" + dom_U_id);
Statement st2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
int j = st2.executeUpdate("insert into tbl_domU_action_history values('" + dom_U_name + "', 1, '" + date + "')");

In the above case, is there any harm in using the same statement st1 for both the executeUpdate() queries? Can I use the same Statement object st1 for another executeQuery()?

+7  A: 

Yes, you can. However, it is very much better to use PreparedStatement to avoid SQL injection vulnerabilities.

Tom Hawtin - tackline
+1  A: 

Whenever you assign something to a reference type, you're replacing the old reference with a new one.

For example...

MyObject obj = new MyObject("foo");
obj = new MyObject("bar");

Would have a now non-referenced instance of MyObject with some property set to "foo" that will eventually be garbage collected.

obj stores a reference to a MyObject with some property set to "bar".

R. Bemrose
+1  A: 

The original point of using prepared statements was to avoid having the database parse and recompile the statement, so it's supposed to be faster.

I had not considered the SQL injection vulnerabilities use, but I'm not sure what, if any data checking is done. I suspect that it's driver-dependant, as the driver implementation is free to just glue the statements together. If anyone has further details, please post.

Steve B.
It appears that the JDBC spec does not require correct handling of special characters. I'd like to hear about any drivers that do not so they can be avoided.
Tom Hawtin - tackline
Steve, the idea re: SQL injection is that using parameterized statements avoids the risk. See http://en.wikipedia.org/wiki/SQL_injection#Preventing_SQL_Injection. Of course you are right that their original purpose was to avoid unnecessary parsing.
Dave Costa
Yes, however as the drivers merely implement the JDBC interface(s) it's unclear to what extent this is actually happening, and it's totally dependent on the driver implementors. I was hoping someone would have more specific knowledge based on having had to deal with this issue in the past.
Steve B.
+1  A: 

I came across the response I was looking for in the Javadocs

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects.

Epitaph
It don't see how this answers the question : should we call the close() method on the Statement ?
watcherFR