[英]Prepared Statement not working even though the same query returns a row when run in toad
I am trying to get a row from DB using preparedStatement using a code which somewhat looks like this: 我正在尝试使用preparedStatement从数据库中获取一行,该代码看起来像这样:
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: 但是通过TOAD执行以下查询时,返回一行:
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). (where子句中的第三个条件是一个字符)。
The two queries are different, in TOAD you're issuing 这两个查询是不同的,在您发出的TOAD中
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
z
与Z
You don't need to specify Oracle CHAR
parameters as Java char
in the setParameter, so changing 您无需在setParameter中将Oracle CHAR
参数指定为Java char
,因此可以进行更改
preparedStatement.setString(3,String.valueOf('Z'));
to either 到
preparedStatement.setString(3,"z");
or 要么
preparedStatement.setString(3,String.valueOf('z'));
will probably work. 可能会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.