dcsimg
Problem with ResultSet in Beans
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mujtaba_Ahmed
Posted On:   Sunday, September 15, 2002 06:56 AM

'showlogin.jsp' is a simple page where a member enters his Id and password. 'Checklogin.jsp' simply verifies the user in a membership table through a bean. 'Login.java' contains a method authenticate() which validates a user Id and password. If user is a member ResultSet will contain non-null value else it will be null. But I am having problem if a user is not a member. here is the code myResultSet=(ResultSet)stmt.executeQuery("SELECT mem_id,password FROM membership where membership.mem_id='"+username+"'"+" and membership.password='"+password+"'"); if(myResultSet!=null){ myResultSet.next(); String id=myResultSet.getString("mem_id"); String pass=myResultSet.getString("pa   More>>

'showlogin.jsp' is a simple page where a member enters his Id and password. 'Checklogin.jsp' simply verifies the user in a membership table through a bean. 'Login.java' contains a method authenticate() which validates a user Id and password. If user is a member ResultSet will contain non-null value else it will be null. But I am having problem if a user is not a member.


here is the code


myResultSet=(ResultSet)stmt.executeQuery("SELECT mem_id,password FROM membership where membership.mem_id='"+username+"'"+" and membership.password='"+password+"'");


if(myResultSet!=null){


myResultSet.next();

String id=myResultSet.getString("mem_id");


String pass=myResultSet.getString("password");


if(pass.equals(password)&&id.equals(username)){


stmt.close();


conn.close();


return new User(username);


}


}//end of if


If it got a null than the server is sending an error saying that error has occured before myResultSet and in root cause it says 'SQLException error'.


Plz let me know what should I do with null values.


here is code in checklogin.jsp


String display="showLogin.jsp";


User user=loginBean.authenticate();


if(user !=null){


session.setAttribute("user",user);


monitor.put(user,session);


session.setMaxInactiveInterval(900);


display="members.jsp";


}


I'll be obliged to you for this favour.

   <<Less

Re: Problem with ResultSet in Beans

Posted By:   Steven_Martin  
Posted On:   Sunday, September 15, 2002 10:10 AM

I see one big mistake here; you only close your statement and conneciton if the user matches. You need to put that in a try{}finally{} block especially to close the connection. You will quickly run out of connections. Look at other JDBC example calls and you will see this convention.




I would confirm that you aren't sending nulls in your query. You shouldn't receive SQL errors like that.
About | Sitemap | Contact