Jonathan Howells wrote:
>
> Can anyone help with this Java Servlet querying a null
> value?
>
> We are making a login page for a website with a MySQL
> database and have the following loop which checks if
> the username and password is already there or not. It
> works if they are but if not it just prints a blank
> screen. We are using JDK 2 on Tomcat servlet runner
> and it does not seem to like the null value. Is this
> a Java problem, how can we test for nulls from the
> database.
>
> Thanks for your help, sorry if this is not a database
> problem.
> Jon
>
> //run this sql query
> rs = stmt.executeQuery ("SELECT firstname,
> username , password FROM member where username = '" +
> username +"' AND password = '"+password+"'");
>
> //dISPLAY THE SQL RESULTS
>
>
> //{
> //Object user= rs.getObject("username"));
> //if (!rs.wasNull())
> //out.println("sorry" + user);
> //}
>
> out.println("<html>");
>
>
> while(rs.next())
> {
> String user;
> String pass;
> if (rs.wasNull()){
> user = "no user name";
> pass = "no password"; }
> else {
> user = rs.getString("username");
> pass = rs.getString("password");
> }
>
> out.println(pass.trim() + ", " + user.trim());
>
> }
> out.println("</body></html>");
>
> }
Hi Jonathan
You got mixed up here.
You talk about NULL values, but mean empty ResultSets.
Try something like this:
public void passwordTest( String username, String password )
{
rs = stmt.executeQuery ("SELECT firstname, username , password FROM member where
username = '" +
username + "' AND password = '" + password + "'");
out.println("<html>");
if (rs.next == false)
{
out.println("Sorry invlid user and password combination!!!");
}
else
{
do
{
String user;
String pass;
user = rs.getString("username");
if (rs.wasNull())
{
user = "no user name";
}
pass = rs.getString("password");
if (rs.wasNull())
{
pass = "no password";
}
out.println(pass.trim() + ", " + user.trim());
}
while(rs.next());
}
out.println("</body></html>");
}
Tschau
Christian