我正在尝试使用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)。
答案 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'
z
与Z
您不需要在setParameter中将Oracle CHAR
参数指定为Java char
,因此更改
preparedStatement.setString(3,String.valueOf('Z'));
到
preparedStatement.setString(3,"z");
或
preparedStatement.setString(3,String.valueOf('z'));
可能会奏效。