java:不支持使用executeQuery(string)方法错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3419266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:09:30  来源:igfitidea点击:

java : use of executeQuery(string) method not supported error?

javasql-server

提问by phill

I'm doing a simple preparedstatement query execution and its throwing me this error: java.sql.SQLException: Use of the executeQuery(string) method is not supported on this type of statement at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.notSupported(JtdsPreparedStatement.java:197) at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:822) at testconn.itemcheck(testconn.java:58)

我正在做一个简单的preparedstatement查询执行,它向我抛出这个错误:java.sql.SQLException:在net.sourceforge.jtds.jdbc.JtdsPreparedStatement.notSupported的这种类型的语句上不支持使用executeQuery(string)方法(JtdsPreparedStatement.java:197) at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:822) at testconn.itemcheck(testconn.java:58)

Any ideas what i'm doing incorrectly? thanks in advance here is the code:

任何想法我做错了什么?预先感谢这里是代码:

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 

采纳答案by Vineet Reynolds

A couple of things are worth checking:

有几件事值得检查:

  1. Use a different alias. Using COUNT as an alias would be asking for trouble.
  2. The query object need not be passed twice, once during preparation of the statement and later during execution. Using it in con.prepareStatement(query);i.e. statement preparation, is enough.
  1. 使用不同的别名。使用 COUNT 作为别名会带来麻烦。
  2. 查询对象不需要传递两次,一次是在准备语句期间,一次是在执行期间。在con.prepareStatement(query);ie 语句准备中使用它就足够了。

ADDENDUM

附录

It's doubtful that jTDS supports usage of the String arg method for PreparedStatement. The rationale is that PreparedStatement.executeQuery() appears to be implemented, whereas Statement.executeQuery(String) appears to have been overriden in PreparedStatement.executeQuery() to throw the stated exception.

jTDS 是否支持对 PreparedStatement 使用 String arg 方法是值得怀疑的。基本原理是 PreparedStatement.executeQuery() 似乎已实现,而 Statement.executeQuery(String) 似乎已在 PreparedStatement.executeQuery() 中被覆盖以引发所述异常。

回答by Drew Wills

So...

所以...

PreparedStatement pstmt = con.prepareStatement(query); 
pstmt.setString(1,itemid);
java.sql.ResultSet rs = pstmt.executeQuery(query);

Unlike Statement, with PreparedStatementyou pass the query sql when you create it (via the Connectionobject). You're doing it, but then you're also passing it again, when you call executeQuery(query).

与 不同StatementPreparedStatement当您创建它时(通过Connection对象)传递查询 sql 。您正在这样做,但是当您调用executeQuery(query).

Use the no-arg overload of executeQuery()defined for PreparedStatement.

使用为 PreparedStatement 定义的 executeQuery()无参数重载

So...

所以...

PreparedStatement pstmt = con.prepareStatement(query); 
pstmt.setString(1,itemid);
java.sql.ResultSet rs = pstmt.executeQuery();