views:

227

answers:

4

Heres a class executing PreparedStatements on a Connection.

public class doSomething {
    private PreparedStatement ps;

    public setPS (Connection conn) throws SQLException {
        String sql = "select * from table where id = ?";
        ps = conn.prepareStatement(sql);
    }

    public void runSomething(String var){
        ps.setString(1,var);
        ResultSet rs = ps.executeQuery();
        ...
    }
}

I call

doSomethingInstance.setPS(conn);
doSomethingInstance.runSomething(var);

from another class and this throws and exception at the

 ResultSet rs = ps.executeQuery();

The exception is SQLException: JZ0S4: Cannot execute an empty (zero-length) query. on a Prepared Statement. I cant understand why. Does anyone see what Iam doing wrong here?

Thanks!

A: 

can you print the contents of sql before executing the query? I suspect it's losing it's contents

A.Rashad
@rohangter: I don't know this API, but are you running a different query than you prepared?
John Saunders
+1  A: 

Go back and copy the code straight from your source file, then edit your question. You have a potential ambiguity: Your first fragment calls the prepared statement "preparedStatement", then you change to "prepareStatement" (without the "d"). Having a clean look at your source code will make isolating the problem easier. Do you have two variables or did you mistype your example?

[later...] Thanks for updating your code. I'm not seeing an obvious problem with it. Have you stepped through it with a debugger (e.g., in Eclipse) to ensure that the two methods are being called as expected?

Bob Kaufman
I apologize. I have fixed the object name. It is basically the same PreparedStatment handle being used by both the methods.
rohangter
A: 

Is var non null? Is var of the correct type(int instead of String I would think?)

John Doe
A: 

This issue has been fixed folks. There was a problem with the database permissions. Catching and printing the stacktrace on

 ResultSet rs = ps.executeQuery();

indicated that the table being accessed by the query was not found. I still have to figure out why the SQLException: JZ0S4: Cannot execute an empty (zero-length) query is being thrown.

Thanks for your time and consideration. Rohan

rohangter