// User Bean
package ritt;
import java.sql.*;
import java.util.*;
public class UserBean
{
private String lname;
private String pwd;
public void setLname(String lname)
{ this.lname = lname; }
public String getLname()
{ return lname; }
public void setPwd(String pwd)
{ this.pwd= pwd; }
public String getPwd()
{ return pwd; }
public boolean login()
{
boolean logged = false;
Connection con = null;
PreparedStatement ps = null;
try
{
con = DBUtil.getConnection();
ps = con.prepareStatement("select lname from users where lname = ? and pwd= ?");
ps.setString(1,lname);
ps.setString(2,pwd);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
if(lname.equals(rs.getString(1)) && pwd.equals(rs.getString(2)))
{
logged=true;
}
}
}
catch(Exception ex)
{
System.out.println( ex.getMessage());
}
finally
{
clean(con,ps);
}
return logged;
} // end of login
public void clean(Connection con, PreparedStatement ps)
{
try
{
if ( ps != null ) ps.close();
if ( con != null) con.close();
}
catch(Exception ex)
{ System.out.println(ex.getMessage()); }
}
public boolean updatePassword(String newpwd)
{
Connection con = null;
PreparedStatement ps= null;
try
{
con = DBUtil.getConnection();
String cmd = "update users set pwd=? where lname = ? ";
ps = con.prepareStatement(cmd);
ps.setString(1,newpwd);
ps.setString(2,lname);
int cnt = ps.executeUpdate();
if ( cnt==1 )
{
pwd = newpwd;
return true;
}
else
return false;
}
catch(Exception ex)
{
System.out.println( ex.getMessage());
return false;
}
finally
{
clean(con,ps);
}
} // end of updatePassword
} // end of bean
database
table users
column-----> lname pwd
values -----> admin a
:guru:
package ritt;
import java.sql.*;
import java.util.*;
public class UserBean
{
private String lname;
private String pwd;
public void setLname(String lname)
{ this.lname = lname; }
public String getLname()
{ return lname; }
public void setPwd(String pwd)
{ this.pwd= pwd; }
public String getPwd()
{ return pwd; }
public boolean login()
{
boolean logged = false;
Connection con = null;
PreparedStatement ps = null;
try
{
con = DBUtil.getConnection();
ps = con.prepareStatement("select lname from users where lname = ? and pwd= ?");
ps.setString(1,lname);
ps.setString(2,pwd);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
if(lname.equals(rs.getString(1)) && pwd.equals(rs.getString(2)))
{
logged=true;
}
}
}
catch(Exception ex)
{
System.out.println( ex.getMessage());
}
finally
{
clean(con,ps);
}
return logged;
} // end of login
public void clean(Connection con, PreparedStatement ps)
{
try
{
if ( ps != null ) ps.close();
if ( con != null) con.close();
}
catch(Exception ex)
{ System.out.println(ex.getMessage()); }
}
public boolean updatePassword(String newpwd)
{
Connection con = null;
PreparedStatement ps= null;
try
{
con = DBUtil.getConnection();
String cmd = "update users set pwd=? where lname = ? ";
ps = con.prepareStatement(cmd);
ps.setString(1,newpwd);
ps.setString(2,lname);
int cnt = ps.executeUpdate();
if ( cnt==1 )
{
pwd = newpwd;
return true;
}
else
return false;
}
catch(Exception ex)
{
System.out.println( ex.getMessage());
return false;
}
finally
{
clean(con,ps);
}
} // end of updatePassword
} // end of bean
database
table users
column-----> lname pwd
values -----> admin a
:guru: