gavin

博客园 首页 新随笔 联系 订阅 管理

============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");
        }
      }
  }
}

posted on 2004-11-19 09:16  gavin  阅读(112)  评论(0)    收藏  举报