wood burning stoves 2.0*
The moose likes JDBC and the fly likes bind variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "bind variable " Watch "bind variable " New topic
Author

bind variable

SANJAY KUMAR
Greenhorn

Joined: Mar 27, 2002
Posts: 25
" WHERE REPORTID LIKE \'%00?\'";
try {
conn = Connection.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1,refreshTime);
System.out.println(sql);
rs = ps.executeQuery();
I am getting bind variable problem .
Any clue


SCJP, SCWCD , SCBCD
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Nope, the bind is the whole thing, not just a string substitution.
You need:
"...WHERE REPORTID LIKE ?";
then:
ps.setInt(1,"%00"+refreshTime);
The Statement will manage adding the single quotes. The only other concern I have is thit it might try to escape the '%', but you'd have to try it first.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"VIKU",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
SANJAY KUMAR
Greenhorn

Joined: Mar 27, 2002
Posts: 25
Hi David,
If you will see
ps.setInt(1,"%00"+refreshTime);
then "%00"+refreshTime is a String and setInt is defined as
setInt(int,int) but here it will be setInt(int,String).
Hope you got.
Any more suggestion.
Thanks
Vivek
Mike Brock
Greenhorn

Joined: Dec 30, 2002
Posts: 15
Well first of all, the LIKE operation in all the RDBMS that I am familiar with is not very applicable to numeric columns within the tables.
user_id LIKE '%00'
Where user_id is an INTEGER column will NOT match, say the number 100. This is because the database does not treat numeric columns like strings, and therefore a wildcard comparison based on a string is useless. Oracle DOES let you use the LIKE comparison operator against numerical fields without error, but you will find yourself getting empty result sets.

Originally posted by VIKU:
Hi David,
If you will see
ps.setInt(1,"%00"+refreshTime);
then "%00"+refreshTime is a String and setInt is defined as
setInt(int,int) but here it will be setInt(int,String).
Hope you got.
Any more suggestion.
Thanks
Vivek
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
Originally posted by VIKU:
" WHERE REPORTID LIKE \'%00?\'";
try {
conn = Connection.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1,refreshTime);
System.out.println(sql);
rs = ps.executeQuery();
I am getting bind variable problem .
Any clue

I don't think you can use a bind variable this way. The bind variable has to be the entire operand for most databases. Usually, you cannot have part of an operand in the SQL text and another part supplied from the bind variable.
Try this:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bind variable
 
Similar Threads
How to use Bind Variable in my java program
how to prevent sql injection
encrypted username breaks sql
Get SQL value out of PreparedStatement - Any API
Scrabble... it only takes a minute...