0

I've spent hours looking at what seems like what should be perfectly working code. The connection.createStatement() version of this method works fine but as soon as I try to convert it over to the better, connection.prepareStatement() version it throws a MySQLSyntaxErrorException and complains about a problem near the '?' character in my query string. The code is posted below and I simply cannot see the problem with it. The database field is VARCHAR and accepts Strings so that is not the problem.

    public Discussion getDbDiscussionInstance(String _instanceId) throws SQLException {

    String qryStr = null;
    PreparedStatement myStmt = null;
    ResultSet myRs = null;

    // Try to build the object with existing data.
    try {
        qryStr = "SELECT assignment_id, discussion_id, section_id, user_id, circle_id, breakout_id, title, description, created, due FROM macb_discussions WHERE instance_id=?";
        myStmt = connection.prepareStatement(qryStr);
        myStmt.setString(1, _instanceId);
        myRs = myStmt.executeQuery(qryStr);
        if (rs.next()) {
            this.discussionId = myRs.getString("discussion_id");
        }
    } catch (SQLException e) {
        dbFunc.catchSQLException(e);
    } finally {
        myRs.close();
        myStmt.close();
    }
}
1
  • can u pls include the exact exception Commented Nov 21, 2014 at 13:57

2 Answers 2

2

Use only myStmt.executeQuery(); without the argument, you have already preperad the statement

Sign up to request clarification or add additional context in comments.

Comments

0

From the docs,

Statement.executeQuery(String sql)

PreparedStatement.executeQuery()

So change your function accordingly. myStmt = connection.prepareStatement(qryStr); to

 myStmt = connection.prepareStatement();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.