81,092
社区成员
发帖
与我相关
我的任务
分享
public class LoginController extends SimpleFormController{
private String showUser;
private String errPage;
public String getErrPage() {
return errPage;
}
public void setErrPage(String errPage) {
this.errPage = errPage;
}
public LoginController(){
setCommandClass(User.class);
}
protected ModelAndView onSubmit(Object command,BindException exception)throws Exception{
User user = (User) command;
LoginDao log = new LoginDao();
if(user.getUser().equals("")){
return new ModelAndView(getErrPage(),"error","用户名不能为空!");
}
if(user.getPwd().equals("")){
return new ModelAndView(errPage,"error","密码不能为空!");
}
if(user.getPwd().length()<6){
return new ModelAndView(errPage,"error","密码不能少于6位!");
}
if(log.isExist(user.getUser(),user.getPwd())){
return new ModelAndView(getShowUser(),"user",user);
}
return new ModelAndView(errPage,"error","账号或密码错误!");
}
public class LoginDao {
public boolean isExist(String username,String password){
Connection conn=DBManager.getConn();
boolean result=false;
String sql="select * from login where name=? and password=?";
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs.next()){
result=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeAll(rs, ps, conn);
}
return result;
}
}
public class Test {
public static void main(String[] args) {
LoginDao log = new LoginDao();
System.out.println(log.isExist("zhangsan","123456"));
}
}