0

java.sql.SQLException: Query does not return results

This is the error I get when running my code.

I am making a tic-tac-toe game to run off of a database. I'm making an empty board with this code.

Any ideas of why the database is not being updated??

//INSERT SPACE
 String space1 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space2 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space3 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space4 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space5 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space6 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space7 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space8 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 String space9 = "INSERT INTO space(row,column,contents) VALUES (?,?,?)";
 /**
 * Inserts empty space into space 1
 */
 try {
 PreparedStatement preparedStatement = connectDatabase.getConnection().prepareStatement(space1);
 preparedStatement.setInt(1,0);
 preparedStatement.setInt(2,0);
 preparedStatement.setString(3, "e");
 ResultSet resultSet = preparedStatement.executeQuery();
 resultSet.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
phflack
2,7271 gold badge12 silver badges21 bronze badges
asked Dec 9, 2017 at 1:20
1
  • What kind of result you're expecting from an INSERT ??? Commented Dec 9, 2017 at 1:37

2 Answers 2

4

You should use preparedStatement.executeUpdate() instead. See the docs here.

The method .executeQuery() is only used for SELECT operations, or in general for any queries that return something. If, instead, you want to modify your database, you must use .executeUpdate() instead.

answered Dec 9, 2017 at 1:22
Sign up to request clarification or add additional context in comments.

Comments

2
ResultSet resultSet = preparedStatement.executeUpdate();
 ^^^^^^

ExecuteUpdate is the method to use for non-results-returning DML.

answered Dec 9, 2017 at 1:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.