我正在执行一个简单的准备好的语句查询,它向我抛出此错误: java.sql.SQLException:net.sourceforge.jtds.jdbc.JtdsPreparedStatement.notSupported(JtdsPreparedStatement.java:197) 的此类语句不支持使用executeQuery(string) 方法。 JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:822) 在 testconn.itemcheck(testconn.java:58)
你知道我做错了什么吗?提前致谢 这是代码:
private static int itemcheck (String itemid ) {
String query;
int count = 0;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = java.sql.DriverManager.getConnection(getConnectionUrl2());
con.setAutoCommit(false);
query = "select count(*) as itemcount from timitem where itemid like ?";
//PreparedStatement pstmt = con.prepareStatement(query);
//pstmt.executeUpdate();
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1,itemid);
java.sql.ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
System.out.println(count);
} //end while
}catch(Exception e){ e.printStackTrace(); }
return (count);
} //end itemcheck
最佳答案
有几件事值得检查:
- 使用不同的别名。使用 COUNT 作为别名会带来麻烦。
- 查询对象不需要传递两次,一次是在准备语句期间,一次是在执行期间。在
con.prepareStatement(query);
中使用它,即语句准备,就足够了。
附录
jTDS 是否支持为PreparedStatement 使用String arg 方法值得怀疑。基本原理是,PreparedStatement.executeQuery() 似乎已实现,而 Statement.executeQuery(String) 似乎已在PreparedStatement.executeQuery() 中被重写以抛出所述异常。
关于java : use of executeQuery(string) method not supported error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55776469/