即使同一查询在toad中运行时返回一行,Prepared Statement也不起作用

时间:2012-03-15 10:08:38

标签: java sql prepared-statement toad

我正在尝试使用prepareStatement从DB获取一行,使用的代码有点像这样:

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");
}

但是通过TOAD执行时的以下查询返回一行:

select * from abc where a='x' and b='y' and c='z';

我在这里做错了什么? (where子句中的第三个条件是char)。

1 个答案:

答案 0 :(得分:1)

两个查询不同,在TOAD中你发出

select * from abc where a='x' and b='y' and c='z'

而从你准备好的陈述中,你做的就是:

select * from abc where a='x' and b='y' and c='Z'

zZ

不同

您不需要在setParameter中将Oracle CHAR参数指定为Java char,因此更改

preparedStatement.setString(3,String.valueOf('Z'));

preparedStatement.setString(3,"z");

preparedStatement.setString(3,String.valueOf('z'));

可能会奏效。