File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Else loop not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Else loop not working" Watch "Else loop not working" New topic
Author

Else loop not working

charu latha
Ranch Hand

Joined: Aug 20, 2002
Posts: 67
<%@ page import="java.sql.*,java.io.*" %>

<%
String username = request.getParameter("username");
String password = request.getParameter("password");

Connection con = null;
Statement stmt = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:malldata");
String sql = "SELECT CUST_ID,USER_ID, PASSWORD FROM CUSTMASTER WHERE USER_ID=? and PASSWORD=?";
ps = con.prepareStatement(sql);
stmt = con.createStatement();
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();

boolean temp = rs.next();
out.println ("the value of the boolean statement is " + temp);
if (rs.next()){
out.println (rs.getObject(1).toString());
out.println (rs.getObject(2).toString());
out.println (rs.getObject(3).toString());
}
else if (!rs.next()) %>
<jsp:forward page="login.html" />
} catch (SQLException e) {
throw new ServletException ("servlet could not display records . " + e);
}
catch (ClassNotFoundException e) {
throw new ServletException ("Jdbc driver not found " + e);
}
My code is given above. It always executes the else loop even if the username and password exist in the database. Am i doing something drastically wrong here???
prabhat kumar
Ranch Hand

Joined: Apr 11, 2001
Posts: 114
yes .. you are doing wrong in else part.
you should simply do like this
since the query is supposed to return only one or less record:
if(rs.next()){//it means user exists in database
//do what you want to do
}else{//means no records returned
//now forward it
}
charu latha
Ranch Hand

Joined: Aug 20, 2002
Posts: 67
ok i got it but i still have the problem of forwarding.
If i use
else { %>
<jsp:forward page="login.html" />
<%(whatever the rest of my code is%>
here the jsp forward tag gets called everytime this jps page is called and so even if i enter a correct value and the result set is true i am not getting my control out of the html page
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Charu
Post the code as it is now so we can see it. Also, if you put it in the UBB code tags ti is easier to read.


Dave
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Hello Charu,
Every call to the method ResultSet.next() advances the cursor by one row. In your code, the first call to next() is at this point
---------------------------
boolean temp = rs.next();
out.println ("the value of the boolean statement is " + temp);
---------------------------
The second call within the condition, if(rs.next()){...}, moves the cursor past the first row and thus returns false.
Use if(temp){...} instead of if(rs.next()){...} and it should work.
Prabhat's suggestion was close; i guess he missed the temp part :-)
-j
charu latha
Ranch Hand

Joined: Aug 20, 2002
Posts: 67
ok here it goes again. my code is shown below:
<%@ page import="java.sql.*,java.io.*" %>

<%
String username = request.getParameter("username");
String password = request.getParameter("password");

Connection con = null;
Statement stmt = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:malldata");
String sql = "SELECT CUST_ID,USER_ID, PASSWORD FROM CUSTMASTER WHERE USER_ID=? and PASSWORD=?";
ps = con.prepareStatement(sql);
stmt = con.createStatement();
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();

if (rs.next()) {
out.println (rs.getObject(1).toString());
out.println (rs.getObject(2).toString());
out.println (rs.getObject(3).toString());
}
else %>
<jsp:forward page="login.html" />
<% } catch (SQLException e) {
throw new ServletException ("servlet could not display records . " + e);
}
catch (ClassNotFoundException e) {
throw new ServletException ("Jdbc driver not found " + e);
}
finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if ( con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
} // close of finally
%>
ok i have attached my code. The jsp forward is called irrespective of the condition. In plain language i would like to know how to forward my control back to my html page because the above else loop functions even if the values are retrieved from the database.
prabhat kumar
Ranch Hand

Joined: Apr 11, 2001
Posts: 114
the problem with jsp:forward is that ..
it ignores the rest code and forwards the request ,
in this your connection is always open even if you closing it in the page.
i have modified the code.

[ October 23, 2002: Message edited by: prabhat kumar ]
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Originally posted by charu latha:
The jsp forward is called irrespective of the condition.

Since you have not used the braces {...} around the else block, the call to forward remains out side of the 'else' and is always being executed. Look at the generated servlet code.
As a rule, always use curly braces to create a block of code when mixing HTML with JSP

-j
[ October 23, 2002: Message edited by: Jignesh Malavia ]
charu latha
Ranch Hand

Joined: Aug 20, 2002
Posts: 67
Thanks for the input. I put the braces and the code now works just fine.
Thanks once again to all who have taken the time out to answer my question
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Else loop not working
 
Similar Threads
getObject () not returning values
Problem with jsp:useBean tag...URGENT
display problem.
add data to database using JSP script
Delete Records from MS Access Database