Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

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 
like image 369
phill Avatar asked Aug 05 '10 21:08

phill


People also ask

What is the return type of executeQuery () method in Java?

Whereas the execute( ) and executeUpdate( ) methods discussed in previous sections return primitive data types -- a boolean and int , respectively -- the method normally used with a SELECT statement, executeQuery( ) , returns a ResultSet object.

What is executeQuery ()?

executeQuery() : This method is used to retrieve data from database using SELECT query. This method returns the ResultSet object that returns the data according to the query.

Can we use executeQuery for update?

executeQuery() was designed to execute query statements so it returns a ResultSet that contains the data returned by the query. The Statement class offers a second method that you use to execute other types of commands ( UPDATE , INSERT , DELETE , and so forth).

What is the return type of statement executeQuery Sqlquery method?

ResultSet executeQuery(String SQL): Returns a ResultSet object.


2 Answers

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.

like image 88
Vineet Reynolds Avatar answered Sep 18 '22 19:09

Vineet Reynolds


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();
like image 41
Drew Wills Avatar answered Sep 18 '22 19:09

Drew Wills