【发布时间】:2011-06-28 22:42:10
【问题描述】:
这是我的静态实用程序:
//String sqlQuery = "select count(name) as num from tbname where name = ?"
//String name = "testString";
private static int correct(Connection connection, String sqlQuery, String name) {
int result = 0;
PreparedStatement statatement = null;
ResultSet rs = null;
try {
statatement = connection.prepareStatement(sqlQuery);
statatement.setString(1, name);
rs = statatement.executeQuery();
while (rs.next()) {
result = rs.getInt("num");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
statatement.close();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
return result;
}
}
上面的方法返回0(结果不正确),但是下面的方法返回'1'(它工作正常,它是相同的sql查询):
//String sqlQuery = "select count(name) as num from tbname where name = 'testString'"
private static int correct(Connection connection, String sqlQuery, String name) {
int result = 0;
PreparedStatement statatement = null;
ResultSet rs = null;
try {
statatement = connection.prepareStatement(sqlQuery);
rs = statatement.executeQuery();
while (rs.next()) {
result = rs.getInt("num");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
statatement.close();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
return result;
}
}
请您给我任何建议,以便我可以解决问题。
PS:我不确定它是否重要,但实际的 streetName - 在 windows-1251 编码(俄语)文本中有一个名称。 PPS:数据库是 Oracle 10。
【问题讨论】:
-
您已经陈述了一些事实,但还没有告诉我们问题所在......
-
当我通过设置 sql 模板 (statement.setString(1, name) 运行 sql 查询时,它无法正常工作。当我运行与动态相同的 sql 查询时,它确实有效。
-
这个查询显示 SQL 编辑器是什么? (尝试 SQL Developer 或类似工具)。
-
是否有任何
SQLException转储到错误输出中? (代码确实存在错误处理和资源管理问题。)
标签: java jdbc prepared-statement