java - My resultset is always returning false -


i have written following code:

string username=request.getparameter("username"); system.out.println(""+username); string password=request.getparameter("password"); system.out.println(""+password); connection con=getconnection.getconnectionbuilder(); system.out.println("inside"); preparedstatement pstmt=con.preparestatement("select password users username=? "); pstmt.setstring(1, "username");  resultset rs=pstmt.executequery();  try {  system.out.println(""+rs.next());     while(rs.next())  {     string pass=rs.getstring(1);     system.out.println(""+pass);     if(pass.equals("password"))     {         out.print("welcome back"+username);     }     else     {         out.print("wrong username/password combination");     } }   }          catch(exception e)    {      e.printstacktrace();    } pstmt.close();  con.close(); 

my rs.next() part getting evaluated false. how come happening so?

don't call rs.next() twice :

 system.out.println(""+rs.next());     while(rs.next()) 

you moving iterator each time call it.

so if query returns 1 row, first rs.next() moves iterator point row, second rs.next() returns false , never enter while loop.

removing system.out.println(""+rs.next()); line should solve problem.

in addition, searching username called "username", doesn't exist, , comparing returned password "password", wrong.

fixing these problems give code :

pstmt.setstring(1, username); resultset rs=pstmt.executequery();  try {   while(rs.next()) {     string pass=rs.getstring(1);     system.out.println(""+pass);     if(pass.equals(password)) {         out.print("welcome back"+username);     } else {         out.print("wrong username/password combination");     }   } }      catch(exception e) {   e.printstacktrace(); } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -