Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PreparedStatement parameter binding

843854Feb 19 2004 — edited Feb 20 2004
Hello,

I am new to JDBC and java technology. I have experience with SQL, PL/SQL and I am quite shocked, that binding parameters using PreparedStatement class is based on parameter index instead of the parameter name.

It is quite common in PL/SQL to use paramter name to bind a value to it.
E.g. if I use query "select * from foo where id = :id and name = :name" I can bind parameters using dbms_sql.bind_variable(query, "id", 5) dbms_sql.bind_variable(query, "name", "london").

However, in java I jave to bind using parameter index e.g query.setInt(1, 5), query.setString(2, new String("london")). Now the problems are
- what if the sql programmer changes code "select * from foo where id = ? and name = ?" to "select * from foo where name = ? and id = ?"
- think of a quite more complicated query, e.g. query with size of few kB - there can be a parameter repeated many times (id, date range etc) so I need to bind this parameter many times, instead of simple one bind by name
- think of a dynamicaly generated query, where it is seriously complicated to follow the parameter indexes (sql block repeating, dynamicaly generated where clausules etc) and again binding by name is much more easier than indexed binding

So my question is: Do you know of any class that supports binding by name ? Is there any workaround how to solve above mentioned issues ?
And finally, what is the reason to use parameters by index instead of naming, which is I belive more obvious in database programming ?

I am looking forward to your answers.

Kindest regards,
Kamil Poturnaj
_______________________________________
Mgr. Kamil Poturnaj, MicroStep-MIS
Ilkovicova 3, 841 04 Bratislava, Slovakia
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 19 2004
Added on Feb 19 2004
4 comments
176 views