[英]Java - Query does not return results
java.sql.SQLException: Query does not return results
java.sql.SQLException:查询不返回结果
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();
}
You should use preparedStatement.executeUpdate()
instead. 您应该改用
preparedStatement.executeUpdate()
。 See the docs here. 请参阅此处的文档。
The method .executeQuery()
is only used for SELECT
operations, or in general for any queries that return something. .executeQuery()
方法仅用于SELECT
操作,或通常用于返回某些内容的任何查询。 If, instead, you want to modify your database, you must use .executeUpdate()
instead. 相反,如果要修改数据库,则必须改用
.executeUpdate()
。
ResultSet resultSet = preparedStatement.executeUpdate();
^^^^^^
ExecuteUpdate
is the method to use for non-results-returning DML. ExecuteUpdate
是用于不返回结果的DML的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.