0
    public static User getUser (User user) throws Exception {
            String sql = "SELECT * FROM login WHERE userID = '" + user.getuserID() + "'";
            //is FROM Correct?
            PreparedStatement pStatement = conn.prepareStatement(sql);
            ResultSet Rset = pStatement.executeQuery(sql);
            Rset.beforeFirst();
            Rset.next();

            int userID = Rset.getInt("userID");
            String email = Rset.getString("email");
            User userReturn = new User (userID, email);
            return userReturn;
        }

Trace: java.sql.SQLException: Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.result.ResultSetImpl.beforeFirst(ResultSetImpl.java:426)
    at DaoLayer.daoImplementation.getUser(daoImplementation.java:28)
    at DaoLayer.DaoTest.testGetUser(DaoTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Jan
  • 13,738
  • 3
  • 30
  • 55

3 Answers3

1

Well, that JDBC / SQL of yours could use some updates.

But first things first. This is what happens:

    ResultSet Rset = pStatement.executeQuery(sql); //Executes the query
    Rset.beforeFirst(); //Moves the cursor backwards before the first row (!)
    Rset.next(); //Goes to the next row

The second line throws an Exeption as the cursor is FORWARD_ONLY. Plus: You don't need that call at all!

This should solve your immediate problem:

    ResultSet Rset = pStatement.executeQuery(sql);
    if(Rset.next()) {
      int userID = Rset.getInt("userID");
      String email = Rset.getString("email");
      User userReturn = new User (userID, email);
      return userReturn;
    } else {
      //Deal with no user found
    }

Now to the general stuff:

  • it's good that you're preparing the Statement. But as Bashir already pointed out: If you go and use executeQuery(sql), which is a member of class Statement, you throw away the precompiled statement.
  • when using PreparedStatement - use bind parameters as well. This makes your code less oen for SQL injection and
  • use try-with-resource to make sure you close everything properly. Hunting down connection leaks or opened cursors is a hassle easily avoided.

In the end you'll probably end up with something like this:

public static User getUser (User user) throws Exception {
    String sql = "SELECT * FROM login WHERE userID = ?;
    try(PreparedStatement pStatement = conn.prepareStatement(sql)) {
      pStatement.setInt(1, user.getuserID());
      try(ResultSet Rset = pStatement.executeQuery()) {  
        if(Rset.next()) {
          int userID = Rset.getInt("userID");
          String email = Rset.getString("email");
          User userReturn = new User (userID, email);
          return userReturn;
        }
      }
    } catch(SQLException e) {
      //Deal with the exception
      e.printStackTrace();
    }
    //Error or no user found
    return null;
}
Jan
  • 13,738
  • 3
  • 30
  • 55
1

When you create a standard ResultSet you can only use rs.next() method to iterate the resulting rows. So rs.beforeFirst() is not allowed. And it is also not necessary in your code. When the ResultSet is created, the cursor is automatically placed before the first row. So, use just if(rs.next()) {...}. When rs.next() returns false after the first invocation, it means that the ResultSet is empty.

paulo116
  • 168
  • 1
  • 8
0

change pStatement.executeQuery(sql) by pStatement.executeQuery()

Also userID is int so no need to use the single quote ' in the query (but this is not the root of the Exception)

I suggest to use an if statement on Rset.next() (which increment cursor, no need to use beforeFirst()) this may cause Exceptions while getting values if the query return nothing.

Use Code as follow

public static User getUser (User user) throws Exception {
        String sql = "SELECT * FROM login WHERE userID = " + user.getuserID();
        //is FROM Correct?
        PreparedStatement pStatement = conn.prepareStatement(sql);
        ResultSet Rset = pStatement.executeQuery();
        if(Rset.next())
        {
             int userID = Rset.getInt("userID");
             String email = Rset.getString("email");
             User userReturn = new User (userID, email);
             return userReturn;
        }
        return null;

    }
Bashir
  • 2,057
  • 5
  • 19
  • 44