0
我正在使用登錄表單。這段代碼是從數據庫中檢查日誌的詳細信息。我能夠在提供正確的用戶名和密碼時登錄,但顯示一個例外。我的代碼如下:爲什麼「不能爲SELECT發出executeUpdate()」SQLException顯示?
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) {
}
}
唯一的例外是:
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$1.handle(Login.java:118)
at login.Login$1.handle(Login.java:113)
爲什麼出此異常?我該如何糾正它?
@jewelsea如果我上面提到的查詢工作正常,那麼prepareStatement2和resultSet2的值是什麼? – TomJ
這是一個完全不同的問題TomJ,請不要在評論中提出新問題。結果集應該包含查詢結果(在這種情況下,用戶名和密碼)。您可以閱讀[基本JDBC教程](http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html)以進一步瞭解結果集。 – jewelsea
@jewelsea還行。謝謝。 – TomJ