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.
The
next()
moves the cursor froward one row from its current position in theresultset
. so its evident thatif(rs.next())
means that if the next row is notnull
(means if it exist), Go Ahead.Now w.r.t your problem,
note that executeQuery(String) is used in case you use a sql-query as string.
Whereas when you use a PreparedStatement, use executeQuery() which executes the SQL query in this
PreparedStatement
object and returns theResultSet
object generated by the query.Solution :
Use :
ResultSet rs = stmt.executeQuery();