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

login code repeats error message

 
jan frank
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

I have this piece of login code for a webpage that words like a charm IF there's only one username in the database table that the website is connected to.

If there are 3 usernames in the database table AND the username & password of the website is incorrect, then the error message is replicated THREE times!!

"Login Failed.Please try Again Login Failed.Please try Again Login Failed.Please try Again."

If there are 4 usernames in the database table and the username and password on trying to log in from the website is incorrect, then the error message is replicated FOUR times...and so on.

Any ideas?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really not a JSP question so off to the JDBC forum we go.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's doing exactly what you told it to:



You are selecting all the usernames and passwords from the table and then looping over them all.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
You are selecting all the usernames and passwords from the table and then looping over them all.

In other words, look into adding a where clause and seeing if any rows are returned rather than looping through them all.
 
Srilakshmi Vara
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are displaying the failure message in the Resultset loop, so it is displaying for each resultset row, which is not meeting your if condition, try to put the message out of the loop only once, by using any boolean variable.
 
vikassheel gupta
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con=DriverManager.getConnection("your driver & port realtedinformateion");
uname=request.getParameter("uname");
upassword=request.getParameter("upassword");

sql="select * from login where acc0='"+uname+"' and acc1='"+upassword+ "' ";
st=con.createStatement();
rs=st.executeQuery(sql);
if(rs.next())
{
response.sendRedirect("dod_auth.jsp");
session.setAttribute("username",uname);
}
else
{
response.sendRedirect("login.jsp?status='failed'");
}

}catch(Exception e)
{
System.out.println("A SQL error is generated :"+e);
}

this will help u alot fine
and dont for get to use Exception
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic