1

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$1.handle(Login.java:118)
at login.Login$1.handle(Login.java:113)

Why this exception is shown ? How can I correct it ?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
TomJ
  • 1,803
  • 14
  • 37
  • 57
  • @jewelsea If the query I mentioned above works correctly, what will be the values of preparedStatement2 and resultSet2 ? – TomJ Feb 23 '14 at 07:19
  • That's a completely different question TomJ, please don't ask new questions in comments. The result set should hold the query results (username and password in this case). You can read the [basic JDBC tutorial](http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html) to understand result sets further. – jewelsea Feb 23 '14 at 07:26
  • 1
    As the question has been significantly edited, it is no longer a duplicate as previously marked. – jewelsea Feb 23 '14 at 10:09

1 Answers1

8

The problem is that you're not using JDBC appropriately. Why are you invoking executeUpdate with a select query? You might want to try executeQuery instead. Here is an example of a simple SQL selection query with JDBC.

Also, be careful to not write code that could be susceptible to SQL Injection.

axiopisty
  • 4,972
  • 8
  • 44
  • 73