JDBC question: Prepared statement class

Argo

Lifer
Apr 8, 2000
10,045
0
0
Is it possible to do something like the code attached below. I'm not sure if you can use '?' parameter within the parentesised list. Unfortunately, I don't have a DB on this machine so I can't test this out myself.

Now second, tougher question. How about if I don't know the number of parameters until runtime. Would it be possible to do that without generating the string at runtime (which defeats the purpose of using a prepared statement). Thanks in advance for help and let me know if I'm not being clear enough.
 

znaps

Senior member
Jan 15, 2004
414
0
0
I don't think there would be a problem with either.

By the way, you could always download HSQL to test any JDBC stuff very easily - it'll create an in memory or on-disk DB at runtime, with no hassles at all.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I'm gonna take my best guesses at these, I'm not really sure :)

You should be able to use ? anywhere where you are inserting actual values. This will ultimately depend on the actual driver that you are using, but I can't see your example being a problem (unless it's a really cruddy driver).

My inclination on the second question would be to generate the question mark list at run time and take the performance hit. I'm not sure, but I believe good drivers will cache your statement and reuse it if you happen to generate the same one twice so you could just go ahead and recreate the PreparedStatement every time and only take the hit for the string comparison. It seems that it might be possible to only put one question mark in and try to substitute it with a List or an Array but I've never seen it so that's purely speculation.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
It seems that it might be possible to only put one question mark in and try to substitute it with a List or an Array but I've never seen it so that's purely speculation.

I think I've tried that a while ago and it didn't work. Of course, there might be a specific semantic to use that, which is what I was wondering.