I am currently getting the error,
java.sql.SQLException: Method 'executeQuery(String)' not allowed on prepared statement.
because I am using
PreparedStatement stmt = conn.prepareStatement(sql);
and also had
ResultSet rs = stmt.executeQuery(sql);
in my code.
I now need to remove the ResultSet line but that leaves me with having to deal with the following code:
if (rs.next()) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("login.successful"));
request.getSession(true).setAttribute("USERNAME", rs.getString("USERNAME"));
request.getSession(true).setAttribute("BALANCE", rs.getString("BALANCE"));
request.setAttribute("msg", "Logged in successfully");
I'm not sure I completely understand what
if (rs.next())
does. Could someone explain this code to me? If I have a better understanding of that I believe I'll have a better idea on how to deal using the PreparedStatement results with the logic being used for rs. Also any help to deal with changing that logic would be greatly appreciated too.
This method returns a boolean value specifying whether the ResultSet object contains more rows. If there are no rows next to its current position this method returns false, else it returns true. Using this method in the while loop you can iterate the contents of the result set.
Moves the cursor forward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only.
The wasNull() method of the ResultSet interface determines whether the last column read had a Null value. i.e. whenever you read the contents of a column of the ResultSet using the getter methods (getInt(), getString etc...) you can determine whether it (column) contains null values, using the wasNull() method.
As to the concrete problem with that SQLException
, you need to replace
ResultSet rs = stmt.executeQuery(sql);
by
ResultSet rs = stmt.executeQuery();
because you're using the PreparedStatement
subclass instead of Statement
. When using PreparedStatement
, you've already passed in the SQL string to Connection#prepareStatement()
. You just have to set the parameters on it and then call executeQuery()
method directly without re-passing the SQL string.
As to the concrete question about rs.next()
, it shifts the cursor to the next row of the result set from the database and returns true
if there is any row, otherwise false
. In combination with the if
statement (instead of the while
) this means that the programmer is expecting or interested in only one row, the first row.
ResultSet#next()
methodIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With