• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use Bind Variable in my java program

 
Chhaya Dhanani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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
 
Kevin Mukhar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic