============LoginBean.java================
package com.eShop.login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginBean {
private Connection conn=null;
public LoginBean(Connection conn) {
this.conn = conn;
}
public String validatePwd(String username,String password){
PreparedStatement ps=null;
ResultSet rs=null;
String validated="error.logon.invalid";
UserForm uf=null;
String sql="select * from Users where UserName=?";
try{
if(conn.isClosed()){
throw new IllegalStateException("error.unexpected");
}
ps=conn.prepareStatement(sql);
ps.setString(1,username);
rs=ps.executeQuery();
if(rs.next()){
if(!rs.getString("password").trim().equals(password)){
return validated;//帐号和密码不匹配
}
else{
validated = "match";//登陆成功
return validated;
}
}else{
validated="error.removed.user";//帐号无效
return validated;
}
}catch(SQLException e){
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}finally{
try{
if(ps!=null)
ps.close();
if(rs!=null)
rs.close();
}catch(SQLException e){
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
}
浙公网安备 33010602011771号