• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use Bind Variable in my java program

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i am writing an aplcation which is following the MVC pattern, where i using classes111.zip i am connecting to oracle
the application fires many queries of the type,
"select colname from tablename where condition='value1' and condition2='value2'"..
of these types.
I want to know how to use bind variables in java queries , maybe use something like
"select colname from tablename where empname:x"
can someone tell how to implement this ?
will can special class be reqd to achieve this?
thanx in advance
Chhaya
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a JDBC PreparedStatement. Here's a simple example:
String sql = "select colname from tablename where colname=?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "some value");
stmt.executeQuery();
The '?' in the Java String represents the bind variable used by the database. Each bind variables is number from 1 to n, counting from the left, regardless of whether it is IN, OUT, or INOUT. The PreparedStatement has many different methods for setting various types of bind variables. Check the javadoc for more info.
 
Chhaya Dhanani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx for th reply..
i have aanother query based on that..
We are using connection pool and before returing the connection object to the pool. we are closing all resultsets and prepared statements in the finally block.
This means that my preparedstaments are lost and no longer stored..
will this defeat the purpose of bind variables ie. usage of prepared statements
or should we not close the preparedestatements..(but not closing is a wrong programming practice)
Pls reply soon
Chhaya
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic