I am trying to get a row from DB using preparedStatement using a code which somewhat looks like this:
PreparedStatement preparedStatement = null;
Connection con = new Connection () //pseudo Code added here
String query = "select * from abc where a=? and b=? and c=?";
preparedStatement = con.prepareStatement(query);
preparedStatement.setString(1,"x");
preparedStatement.setString(2,"y");
preparedStatement.setString(3,String.valueOf('Z'));
ResultSet resultset = preparedStatement.executeQuery();
if(resultset.next() == false)
{
throw new exception("No records fetched");
}
but the following query when executed via TOAD, returns a row:
select * from abc where a='x' and b='y' and c='z';
What am I doing wrong here? (The third condition in where clause is a char).
The two queries are different, in TOAD you're issuing
select * from abc where a='x' and b='y' and c='z'
whereas from your prepared statement you're doing the equivalent of:
select * from abc where a='x' and b='y' and c='Z'
z
is not the same as Z
You don't need to specify Oracle CHAR
parameters as Java char
in the setParameter, so changing
preparedStatement.setString(3,String.valueOf('Z'));
to either
preparedStatement.setString(3,"z");
or
preparedStatement.setString(3,String.valueOf('z'));
will probably work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.