【发布时间】:2009-10-01 10:25:41
【问题描述】:
类似的问题: Strange problem with JDBC, select returns null 但人们并没有要求这个。
我的代码:
public int myMethod(String day) throws SQLException{
String sql = "Select count(*) from MyTable WHERE someColumn = " + day;
Connection connection = ConnFactory.get();
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
int ret = -1;
try{
prepareStatement = connection.prepareStatement(sql);
resultSet = prepareStatement.executeQuery(sql);
if(resultSet.next()){
ret = resultSet.getInt(1);
}
}
catch(SQLException sqle){
// closing statement & ResultSet, log and throw exception
}
finally{
// closing statement & ResultSet
}
ConnFactory.kill(connection);
return ret;
}
此代码始终返回 0。我尝试在执行前记录 sql,并尝试在 SQLdeveloper 中运行它并获得正确的值(超过 100)。
当我删除 WHERE 时,sql = "Select count(*) from MyTable 查询返回表中所有行的数量。
我将 Oracle 10g 与 ojdbc-14.jar(来自 maven repo 的最新版本)和 Java 6 一起使用。
【问题讨论】:
-
如果返回 0,则没有与您的 WHERE 匹配的行。干净利落。在客户端(例如 SQLDeveloper)中运行相同的查询并查看结果。