Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi i want to do an if statement her

Status
Not open for further replies.

technohead

Technical User
Jan 12, 2002
64
0
0
US
hi i want to do an if statement here so that if the password matches that that was entered then redirect the user to a certain page, which i have done. how do i i do an else part of the statement to say if its not the same print out sorry, wrong password. i have these returning variables to a servlet. im also not sure if i have the return statement in the rigth place cos its giving me back null.

thanks.



public String getType(String CustomerName, String CustomerPwd)
{
if(userPwd.equals(CustomerPwd))
{
try
{
String userType;
con = dbcon.getConnection();
String sql = ("Select ut_userType from userTable where ut_userName = '"+CustomerName+"' and ut_password = '"+CustomerPwd+"'");
Statement stat = con.createStatement();
ResultSet rs1 = stat.executeQuery(sql);
while(rs1.next())
{
userType = rs1.getString("ut_userType");
}
}
catch(SQLException e)
{
e.printStackTrace();
}

}
return userType;
}
 
Hi technohead,

The else condition goes after the closing brace of the if statement.

I could be wrong but it looks as though you may have another userType defined as a class member (inside the class but not inside a method). If so, the rs1.getString line will set the method userType and not the class level one. The member userType will go out of scope at the end of the if-statement and the method will return the class userType. I am guessing a bit here but I do not see how the method compiles at present with userType defined in the if-statment. Try removing the line 'String userType;'.

public String getType(String CustomerName, String CustomerPwd)
{
// String userType;
if(userPwd.equals(CustomerPwd))
{
try
{
con = dbcon.getConnection();
String sql = ("Select ut_userType from userTable where ut_userName = '"+CustomerName+"' and ut_password = '"+CustomerPwd+"'");
Statement stat = con.createStatement();
ResultSet rs1 = stat.executeQuery(sql);
while(rs1.next())
{
userType = rs1.getString("ut_userType");
}
}
catch(SQLException e)
{
e.printStackTrace();
}

}
else
{
// Print your message here
}
return userType;
}

Hope this help. Let me know if I have misunderstood the situation.

Regards,
scrat
 
yep that works fine,
alls i want now is to do with the else part. when i say
else
{
System.out.println("Wrong password")
}
it prints it out to my tomcat command prompt.
however i want to pass it back to my servlet so that i can print it out to my screen using
wr.println(&quot;<h1>&quot;+userType+&quot;</h1>&quot;);
the variable should be returned from the page with the code above. can i do this by saying
else
{
return some variable
}

i dont know if i can do this.. any ideas would be good
 
I would say that the reason why
Code:
userType
is returning null is because your
Code:
if
statement never returns true - ie -
Code:
userPwd
never equals
Code:
CustomerPwd
.

You need to look at what values these two variables are being set to before you execute your
Code:
if
statement to see what is going on ....

Ben
 
In the else statement, you can set 'userType' to your required message and return that - because the method returns a String object - this can then be posted out to your servlet page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top