Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Else loop not working

 
charu latha
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ 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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic