Java 为什么“无法为 SELECT 发出 executeUpdate()”SQLException 显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21965219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why "cannot issue executeUpdate() for SELECTs" SQLException showing?
提问by TomJ
I am working with a log in form. This code is to check the log in details from the database. I am able to log in when the provide the correct username and password, but an exception is showing. My code is below :
我正在使用登录表单。此代码用于从数据库中检查登录详细信息。我可以在提供正确的用户名和密码时登录,但显示异常。我的代码如下:
public void signin() throws ClassNotFoundException, SQLException
{
try {
Class.forName("com.mysql.jdbc.Driver");
connect2 = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mysql?"
+ "user=root&password=virus");
statement2 = connect2.createStatement();
preparedStatement2 = connect2
.prepareStatement("select username,password from "+t11.getText()+";");
resultSet2 = preparedStatement2.executeQuery();
writeResultSet(resultSet2);
preparedStatement2.executeUpdate();
}
catch (ClassNotFoundException | SQLException e) {
throw e;
} finally {
close1();
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
String username = resultSet.getString("username");
String password = resultSet.getString("password");
String usr = (String)t11.getText();
String pwd = (String)pw11.getText();
if( t11.getText().equals(username) && pwd.equals(password))
{
((Stage)b1.getScene().getWindow()).setScene(new Scene(new UserPage()));
}
else
{
//...THIS IS WHERE I WANT TO DISPLAY THE ALERT BOX
}
}
}
private void close1() {
try {
if (resultSet2 != null) {
resultSet2.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (SQLException e) {
}
}
The exception is :
例外是:
java.sql.SQLException: Can not issue executeUpdate() for SELECTs
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2416)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at login.Login.signin(Login.java:159)
at login.Login.handle(Login.java:118)
at login.Login.handle(Login.java:113)
Why this exception is shown ? How can I correct it ?
为什么显示此异常?我该如何纠正?
采纳答案by axiopisty
The problem is that you're not using JDBC appropriately. Why are you invoking executeUpdatewith a select query? You might want to try executeQueryinstead. Here is an exampleof a simple SQL selection query with JDBC.
问题是您没有正确使用 JDBC。为什么要executeUpdate使用选择查询进行调用?您可能想尝试一下executeQuery。下面是一个使用 JDBC 的简单 SQL 选择查询的示例。
Also, be careful to not write code that could be susceptible to SQL Injection.
另外,请注意不要编写容易受到 SQL 注入影响的代码。

