Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

problem in bean

  Asked By: Fabiana    Date: Feb 01    Category: Java    Views: 543
  

the problem here is though i m getting right values from databut the
authentication(String username2,String password2)

is always returning false value

i passed the values in this method

login status<%=login.authenticate(u1,p1)%>

where u1 & p1 are

<%String u1=request.getParameter("username");

String p1=request.getParameter("password");

%>

which is passed here from login1.jsp



the authentication method is

public boolean authenticate(String username2, String password2)

{

Connection con= null;

Statement stmt = null;

ResultSet rs = null;

String DbUserName="";

String DbPassword="";

String finalUser="";

try {

Context initctx = new InitialContext();

Context envctx = (Context)initctx.lookup("java:comp/env");

DataSource ds = (DataSource)envctx.lookup("jdbc/inventory");

con = ds.getConnection();



String query="select * from Registration;";



stmt = con.createStatement();

rs = stmt.executeQuery(query);

rs.next();

while(rs.next())

{

DbUserName=rs.getString(1);

DbPassword=rs.getString(2);

if (username2.equals(DbUserName) && password2.equals(DbPassword))

{

break; }

}

return true;

}

catch(Exception e)

{ e.printStackTrace();

return false;

}

/*
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package beans;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Login
{
private String username = "";
private String password = "";

public Login()
{
}
public void setUsername(String username)
{
this.username = username;
}

public void setPassword(String password)
{
this.password = password;
}

public String getUsername()
{
return username ;
}
public String getPassword()
{
return password;
}
public boolean authenticate(String username2, String password2)
{
Connection con= null;
Statement stmt = null;
ResultSet rs = null;
String DbUserName="";
String DbPassword="";
String finalUser="";
try {
Context initctx = new InitialContext();
Context envctx = (Context)initctx.lookup("java:comp/env");
DataSource ds = (DataSource)envctx.lookup("jdbc/inventory");

con = ds.getConnection();

String query="select * from Registration;";


stmt = con.createStatement();

rs = stmt.executeQuery(query);
rs.next();
while(rs.next())
{
DbUserName=rs.getString(1);
DbPassword=rs.getString(2);
if (username2.equals(DbUserName) && password2.equals(DbPassword))
{
break; }
}
return true;
}
catch(Exception e)
{ e.printStackTrace();
return false;
}
}
}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Damon Jones     Answered On: Feb 01

Remove rs.next() before the while loop. Anyway rs.next() is there in while loop
condition, it will work.

Instead of checking with while loop you can do the following

Sql = query="select * from Registration where username=<username> and password  =
<password>";

If (rs.next()) return  true;
else return false;

 
Answer #2    Answered By: Varun Mehta     Answered On: Feb 01

Have u checked what u r getting in string  u1 and p1
and the second thing is that why u r using rs.next()
two times???

rs.next();
>
> while(rs.next())
>
> {

...
..
}
Don't fetch all records for authentication  you can
modiff your query like this one

String query="select count(*) from Registration where
user=u1 and password=p1;";
if(rs.next())
{
// if found 1 than user exists //rs.getInt..(1);
}
For better performance u can also go for
preparedstatement instead of statement.
if your code is for learning purpose than it is
OK......

 
Didn't find what you were looking for? Find more on problem in bean Or get search suggestion and latest updates.




Tagged: