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

前端 未结 2 1060
梦毁少年i
梦毁少年i 2021-01-18 10:15

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

相关标签:
2条回答
  • 2021-01-18 10:44

    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.

    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.

    0 讨论(0)
  • 2021-01-18 10:48

    So...

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

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

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

    So...

    PreparedStatement pstmt = con.prepareStatement(query); 
    pstmt.setString(1,itemid);
    java.sql.ResultSet rs = pstmt.executeQuery();
    
    0 讨论(0)
提交回复
热议问题