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.
First thing, you don't need to write
just write
The above mentioned syntax is used for Statements not for PreparedStatement.
Second thing, rs.next() checks if the result set contains any values or not. It returns a boolean value as well as it moves the cursor to the first value in the result set because initially it is at BEFORE FIRST Position. So if you want to access first value in result set, you need to write rs.next().
As to the concrete problem with that
SQLException
, you need to replaceby
because you're using the
PreparedStatement
subclass instead ofStatement
. When usingPreparedStatement
, you've already passed in the SQL string toConnection#prepareStatement()
. You just have to set the parameters on it and then callexecuteQuery()
method directly without re-passing the SQL string.See also:
As to the concrete question about
rs.next()
, it shifts the cursor to the next row of the result set from the database and returnstrue
if there is any row, otherwisefalse
. In combination with theif
statement (instead of thewhile
) this means that the programmer is expecting or interested in only one row, the first row.See also:
ResultSet#next()
methodThe
next()
method (offcial doc here) simply move the pointer of the result rows set to the next row (if it can). Anyway you can read this from the offcial doc as well:This method return true if there's another row or false otherwise.
Since Result Set is an interface, When you obtain a reference to a ResultSet through a JDBC call, you are getting an instance of a class that implements the ResultSet interface. This class provides concrete implementations of all of the ResultSet methods.
Interfaces are used to divorce implementation from, well, interface. This allows the creation of generic algorithms and the abstraction of object creation. For example, JDBC drivers for different databases will return different ResultSet implementations, but you don't have to change your code to make it work with the different drivers
In very short, if your ResultSet contains result, then using rs.next return true if you have recordset else it returns false.
Look at the picture, it's a result set of a query select * from employee
and the next() method of ResultSet class help to move the cursor to the next row of a returned result set which is rs in your example.
:)
I'm presuming you're using Java 6 and that the ResultSet that you're using is a
java.sql.ResultSet
.The JavaDoc for the ResultSet.next() method states:
So,
if(rs.next(){ //do something }
means "If the result set still has results, move to the next result and do something".As BalusC pointed out, you need to replace
with
Because you've already set the SQL to use in the statement with your previous line
If you weren't using the
PreparedStatement
, thenResultSet rs = stmt.executeQuery(sql);
would work.