2

I am trying to query the database using JDBC and using the following commands

CallableStatement call_state =null;
call_state =con.prepareCall("UPDATE district SET d_next_o_id = ?+1 WHERE d_id = ? AND d_w_id = ?");
call_state.setInt(1, d_next_o_id);
call_state.setInt(2, d_id);
call_state.setInt(3, d_w_id);           
rs=call_state.executeQuery();`

I got an exception: java.sql.SQLException: Cannot submit empty query.

3
  • 1
    Try using a PreparedStatement instead of a CallableStatement. Commented Oct 26, 2016 at 13:06
  • 2
    Use a PreparedStatement and PreparedStatement.executeUpdate(). An update statement does not return a ResultSet Commented Oct 26, 2016 at 13:51
  • 1
    CallableStatement is used to call procedures and functions, not to run queries. Follow the advice from the previous comments and use PreparedStatement. Commented Oct 26, 2016 at 13:54

1 Answer 1

3

Even though CallableStatement (CS) is the "interface used to execute SQL stored procedures", it is also a subinterface of PreparedStatement. For this reason, any CS implementation that abides to the Liskov Substitution Principle should also be able to execute normal UPDATE, SELECT or DELETE operations (anything a plain PreparedStatement can do).

The actual problem with your code is on the last line of the snippet you've provided; you're using executeQuery() on an UPDATE statement, which doesn't work. Instead, you should use either execute() or executeUpdate(). So changing that line into

call_state.executeUpdate();

should work.

If you want to do this "correctly", you should be idiomatic and use a PreparedStatement instead of CallableStatement – because you're not invoking any stored procedure. Additionally, you should take care of closing resources by using the try-with-resources statement:

try (PreparedStatement ps = con.prepareStatement(
        "UPDATE district SET d_next_o_id = ?+1 WHERE d_id = ? AND d_w_id = ?")) {
    ps.setInt(1, d_next_o_id);
    ps.setInt(2, d_id);
    ps.setInt(3, d_w_id);           
    ps.executeUpdate();
} catch (SQLException e) {
  // handle it
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.