登錄驗證

數據庫

/*
 * Created on 2005/07/07.
 * Copyright by 北京五嶽.
 * All right reserved.
 */
package admit.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;

/**
 * <p>Title: DBConnection</p>
 * <p>Description: 讀取DB連接文件</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: 五嶽</p>
 *
 * @author gongjian
 * @version 1.0
 */
public class DBConnection {
    private static boolean isLoaded = false;
    private static String strErrorMessage = "";

    private static String dbDriver = "oracle.jdbc.driver.OracleDriver";
    private static String dbUrl = "jdbc:oracle:thin:@192.168.0.100:1521:adminSys";
    private static String dbUser = "";
    private static String dbPassword = "";
   
    //數據庫連接的最大數
    private static int dbMaxConnects = 0;

 //一個集合,用於承載空閒的連接
    private Vector freeConnections;
   
 //當前和數據庫連接的數量
    private int CurrentConnectionNumber;

 //自身類的一個靜態實例
    private static DBConnection instance = null;

 /**
  * 重寫構造函數 判斷讀取到的DB連接驅動可不可以使用
  */
    public DBConnection() {
        freeConnections = new Vector();
        if (getDBEnviroment()) {
            try {
                Class.forName(dbDriver);
                isLoaded = true;
            }
            catch (Exception e) {
                //System.out.print(e);
                isLoaded = true;
                strErrorMessage = "ClassLoader..." + e.getMessage();
            }
        }
    }

 /**
  * 同步方法,得到自身的靜態實例
  */
    public static synchronized DBConnection getInstance() {
        if (instance == null) {
            instance = new DBConnection();
        }
        return instance;
    }

 /**
  * 同步方法,得到空閒的連接
  *
  * @return Connection
  */
    public synchronized Connection getConnection() {
        Connection con = null;
        if (isLoaded) {
   /**
    * 如果還有空閒的連接,從中取出一個連接,然後將其從空閒連接的集合中刪掉
    * 如果連接已滿,建立一個新的連接
    */
            if (freeConnections.size() > 0) {
                con = (Connection) freeConnections.firstElement();
                freeConnections.removeElementAt(0);
                try {
                    if (con.isClosed()) {
                        con = getConnection();
                    }
                }
                catch (SQLException e) {
                    con = getConnection();
                }
            }
            else if (dbMaxConnects == 0 || CurrentConnectionNumber < dbMaxConnects) {
                con = newConnection();
            }

            if (con != null) {
                CurrentConnectionNumber++;
            }
        }
        else {
            return null;
        }

        return con;
    }

 /**
  * 新建一個連接
  *
  * @return Connection
  */
    private Connection newConnection() {
        Connection con = null;
        try {
            if (dbUser == null) {
                con = DriverManager.getConnection(dbUrl);
            }
            else {
                con = DriverManager.getConnection(dbUrl, dbUser, dbPassword);

            }
        }
        catch (SQLException e) {
            System.err.println("newConnection: " + e.getMessage());
            return null;
        }
        return con;
    }

 /**
  * 同步方法,釋放連接,將空閒的連接放到集合中,當前連接數減1
  *
  * @param con Connection
  */
    public synchronized void freeConnection(Connection con) {
        freeConnections.addElement(con);
        CurrentConnectionNumber--;
        notifyAll();
    }

 /**
  * 同步方法,關閉連接池中的所有連接
  */
    public synchronized void release() {
        Enumeration allConnections = freeConnections.elements();
        while (allConnections.hasMoreElements()) {
            Connection con = (Connection) allConnections.nextElement();
            try {
                con.close();
            }
            catch (SQLException e) {
                System.err.println("releaseAllConnection: " + e.getMessage());
            }
        }
        freeConnections.removeAllElements();
    }

 /**
  * 同步方法,得到錯誤信息
  *
  * @return String 錯誤信息
  */
    public static synchronized String getErrorMessage() {
        return strErrorMessage + " " + dbDriver + " " + dbUrl + " " + dbUser + " " +
                dbPassword + " " + String.valueOf(dbMaxConnects);
    }

 /**
  * 得到數據庫的連接信息
  *
  * @return true  提取信息成功
  *         false 提取信息失敗
  */
    private static boolean getDBEnviroment() {
        EnviromentLoader sysFM = new EnviromentLoader();
        if (!sysFM.isEnabled()) {
            strErrorMessage = sysFM.getErrorMessage();
            System.err.println(strErrorMessage);
            return false;
        }

        dbDriver = EnviromentLoader.getDBConnectDriver();
        dbUrl = EnviromentLoader.getConnectionString();
        dbUser = EnviromentLoader.getDBUser();
        dbPassword = EnviromentLoader.getDBPassword();
        dbMaxConnects = sysFM.getDBMaxConnects();

        if (dbDriver.compareTo("") == 0 || dbUrl.compareTo("") == 0 ||
            dbUser.compareTo("") == 0 || dbPassword.compareTo("") == 0 ||
            dbMaxConnects == -1) {
            strErrorMessage = sysFM.getErrorMessage();
            return false;
        }

        return true;
    }

}

/*
 * Created on 2005/07/07.
 * Copyright by 北京五嶽.
 * All right reserved.
 */
package admit.common;

import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;

/**
 * <p>Title: EnviromentLoader</p>
 * <p>Description: 讀取properties</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company:wuyue</p>
 *
 * @author gongjian
 * @version 1.0
 */
public class EnviromentLoader {

 // 本類名稱的字符串
 private static final String THIS_CLASS = "EnviromentLoader";

 // 錯誤代碼的字符串
 private static String strErrorCode = "";

 // 錯誤信息的字符串
 private static String strErrorMessage = "";

 // 代表文件讀取狀態的字符串
 private static String FILE_READING_STATUS;

 // 要讀取的.properties文件,本類中讀取的是db.properties
 private static String CONFIG_BUNDLE_NAME = "db";

 /**
  * DATABASE CONNECTION VARS (用於數據庫連接的變量定義)
  */
 // the DB Connection Driver String
 // etc. oracle.jdbc.driver.OracleDriver
 static String strDBConnectDriver = "";

 // the DB Connection URL
 // etc. jdbc:oracle:thin:@192.168.0.100:1521:admit
 static String strDBConnectURL = "";

 // the DB Connection Max Connnects
 // etc. 50
 static String strDBMaxConnects = "0";

 // the DB Connection User
 // etc. admit
 static String strDBUser = "";

 // the DB Connection Password
 // etc. wuyue
 static String strDBPassword = "";

 /**
  * @author gongjian
  *
  * 無參數的構造方法
  */
 public EnviromentLoader() {
 }
 
 //定義靜態塊(初始化類的時候即會調用得到數據的方法getEnvironment)
 static {
  getEnvironment();
 }

 /**
  * Static method. Get System Vars From Defination File
  * 從定義好的property文件中讀取數據
  */
 private static void getEnvironment() {
  PropertyResourceBundle configBundle = (PropertyResourceBundle) PropertyResourceBundle
    .getBundle(CONFIG_BUNDLE_NAME);
  if (configBundle == null) {
   FILE_READING_STATUS = "FAILED";
   retMessageFormat("1001", "getEnvironment() :system file open error");
   return;
  }

  try {
   strDBConnectDriver = configBundle.getString("DBConnectDriver");
   strDBConnectURL = configBundle.getString("DBConnectURL");
   strDBUser = configBundle.getString("DBUser");
   strDBPassword = configBundle.getString("DBPassword");
   strDBMaxConnects = configBundle.getString("DBMaxCount");

  } catch (MissingResourceException e) {
   FILE_READING_STATUS = "FAILED";
   retMessageFormat("1002", "getEnvironment() :" + e.getMessage());
   return;
  }

  FILE_READING_STATUS = "FINISHED";

 }

 /**
  * 判斷文件讀取的狀態
  *
  * @return true  讀取成功
  *      false 讀取失敗
  */
 public boolean isEnabled() {
  if (FILE_READING_STATUS.compareTo("FINISHED") == 0) {
   return true;
  } else {
   return false;
  }
 }

 /**
  * 得到錯誤的信息
  *
  * @return String 錯誤信息字符串
  */
 public String getErrorMessage() {
  return strErrorMessage;
 }

 /**
  * 得到錯誤的代碼
  *
  * @return String 錯誤信息代碼字符串
  */
 public String getErrorCode() {
  return strErrorCode;
 }

 /**
  * 得到讀取的數據庫連接驅動
  *
  * @return String 數據庫連接驅動字符串
  */
 public static String getDBConnectDriver() {
  return strDBConnectDriver;
 }

 /**
  * 得到讀取的數據庫連接內容
  *
  * @return String 數據庫連接字符串
  */
 public static String getConnectionString() {
  return strDBConnectURL;
 }

 /**
  * 得到讀取的數據庫連接用戶
  *
  * @return String 數據庫連接用戶字符串
  */
 public static String getDBUser() {
  return strDBUser;
 }

 /**
  * 得到讀取的數據庫連接密碼
  *
  * @return String 數據庫連接密碼字符串
  */
 public static String getDBPassword() {
  return strDBPassword;
 }

 /**
  * 得到讀取的數據庫最大連接數
  *
  * @return int 數據庫最大連接數字符串 (-1代表着無限制)
  */
 public int getDBMaxConnects() {
  if (strDBMaxConnects != null && strDBMaxConnects.compareTo("") != 0) {
   return Integer.parseInt(strDBMaxConnects);
  } else {
   return -1;
  }
 }

 /**
  * 用傳入的字符串格式化錯誤代碼和錯誤信息
  *
  * @param code 傳入的錯誤代碼
  * @param msg 傳入的錯誤信息
  */
 private static void retMessageFormat(String code, String msg) {
  strErrorCode = code;
  strErrorMessage = THIS_CLASS + "-->" + msg;
 }

}


public class UserControl {


 String USER_ID = null;

 String SECTION_CODE = null;

 String USER_NAME = null;

 String EMAIL = null;

 String ADMIN_AUTH = null;

 String AUTH_1 = null;

 String AUTH_2 = null;

 String AUTH_3 = null;

 String AUTH_4 = null;

 String CREATE_DATE = null;

 String UPDATE_DATE = null;

 

 public String getADMIN_AUTH() {
  return ADMIN_AUTH;
 }


 public String getAUTH_1() {
  return AUTH_1;
 }


 public String getAUTH_2() {
  return AUTH_2;
 }


 public String getAUTH_3() {
  return AUTH_3;
 }


 public String getAUTH_4() {
  return AUTH_4;
 }


 public String getCREATE_DATE() {
  return CREATE_DATE;
 }


 public String getEMAIL() {
  return EMAIL;
 }


 public String getSECTION_CODE() {
  return SECTION_CODE;
 }


 public String getUPDATE_DATE() {
  return UPDATE_DATE;
 }


 public String getUSER_ID() {
  return USER_ID;
 }


 public String getUSER_NAME() {
  return USER_NAME;
 }


 public void setADMIN_AUTH(String string) {
  ADMIN_AUTH = string;
 }


 public void setAUTH_1(String string) {
  AUTH_1 = string;
 }


 public void setAUTH_2(String string) {
  AUTH_2 = string;
 }


 public void setAUTH_3(String string) {
  AUTH_3 = string;
 }


 public void setAUTH_4(String string) {
  AUTH_4 = string;
 }


 public void setCREATE_DATE(String string) {
  CREATE_DATE = string;
 }


 public void setEMAIL(String string) {
  EMAIL = string;
 }


 public void setSECTION_CODE(String string) {
  SECTION_CODE = string;
 }


 public void setUPDATE_DATE(String string) {
  UPDATE_DATE = string;
 }


 public void setUSER_ID(String string) {
  USER_ID = string;
 }


 public void setUSER_NAME(String string) {
  USER_NAME = string;
 }


*/

public class UserControl{
 
 String USER_ID=null;
 
 String USER_NAME=null;
 
 String PURVIEW=null;
 
 String PWD=null;
 
 /*
  * 返回USERID
  */
 public String getUSER_ID(){
  return USER_ID;
 }
 
 /*
  * 設置USERID
  */
 public void setUSER_ID(String strUserId){
  USER_ID=strUserId;
 }
 
 /*
  * 返回USERNAME
  */
 public String getUSER_NAME(){
  return USER_NAME;
 }
 
 /*
  * 設置USERNAME
  */
 public void setUSER_NAME(String strUserName){
  USER_NAME=strUserName;
 }
 
 /*
  * 返回密碼
  */
 public String getPWD(){
  return PWD;
 }
 
 /*
  * 設置密碼
  */
 public void setPWD(String strPWD){
  PWD=strPWD;
 }
 
 /*
  * 返回權限
  */
 public String getPURVIEW(){
  return PURVIEW;
 }
 
 /*
  * 設置權限
  */
 public void setPURVIEW(String strPURVIEW){
  PURVIEW=strPURVIEW;
 }
 
 
}

⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒⇒

public class LoginDAO extends AdmitDAO {

 /**
  *
  */
 public LoginDAO() {
 }

 public UserControl getUserControl(String userid, String password)
  throws Exception {
   
  String sql=
   "SELECT USER_ID,"
    + "USER_NAME,"
    + "PURVIEW"
    + " FROM ADMIT.USER_INFO"
    + " WHERE USER_ID= ?"
    + " AND PWD= ?";      
  DBConnection dbc = null;
  Connection conn = null;
  UserControl userControl = null;
  try {
   dbc = DBConnection.getInstance();
   conn = dbc.getConnection();

   if (conn == null) {
    throw new Exception("Connection is null");
   }

   PreparedStatement preStmt = conn.prepareStatement(sql);
   preStmt.setString(1, userid);
   preStmt.setString(2, password);

   ResultSet result = preStmt.executeQuery();

   while (result.next()) {
    userControl = new UserControl();
    userControl.setUSER_ID(result.getString("USER_ID"));
    userControl.setUSER_NAME(result.getString("USER_NAME"));
    userControl.setPURVIEW(result.getString("PURVIEW"));
   }
   if (preStmt != null) {
    preStmt.close();
   }
   if (result != null) {
    result.close();
   }

  } catch (SQLException e) {
   throw e;
  } finally {
   if (conn != null) {
    dbc.freeConnection(conn);
   }
  }

  return userControl;
 }

}

import admit.action.AdmitActionContext;
import admit.db.LoginDAO;
import admit.db.util.UserControl;
import admit.form.LoginForm;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author baichongxian
 * @version 1.0
 * 登錄按鈕按下時,業務處理。
 */
public class LoginBl extends AdmitBl {

 /**
  * 構造器01
  */
 public LoginBl() {
  super();
 }

 /**
  * 構造器02
  * @param blContext
  */
 public LoginBl(AdmitActionContext blContext) {
  super(blContext);
 }

 /**
  * 用戶登陸檢查
  * @return  0  正常
  *                1  異常
  *               2  登陸不成功
  */
 public int doBussiness(LoginForm form) {
  log.debug("start");
  try {

   LoginDAO dao = new LoginDAO();
   UserControl user =
    dao.getUserControl(form.getTxt_user(), form.getTxt_pwd());

   if (user == null) {
    log.error("用戶名不存在! 用戶ID=" + form.getTxt_user());
    this.blCtx.setSessionData("login", "0");
    return 2;
   } else {
    this.blCtx.setSessionData("login", "1");
    this.blCtx.setSessionData("user", user.getUSER_NAME());
    //login time
    SimpleDateFormat sdf =
     new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = Calendar.getInstance().getTime();
    String strFormatTime = sdf.format(date);
    this.blCtx.setSessionData("logintime", strFormatTime);
   }
   
  } catch (Exception e) {
   log.error(e);
   this.blCtx.setRequestData("error_info", e.getMessage());
   return 1;
  }
  log.debug("end");
  return 0;
  
  
 }

}

action

/*
 * 項目名 :北京五嶽管理系統
 * 副系統名 :共同處理
 * class名 :LoginAction.java
 * version :1.0
 * 日期 :2005/07/19
 * 著作權 :Copyright beijingwuyue 2005, All rights reserved.
 */

package admit.action;

import admit.bl.LoginBl;
import admit.form.AdmitForm;
import admit.form.LoginForm;

/*
 * <p>Title: LoginAction.java </p>
 * <p>Description: 轉移到幫助頁面</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: 五嶽</p>
 *
 * @author baichongxian
 * @version 1.0
 */
public class LoginAction extends AdmitAction {
 
 /*
  * 定義Action的execute方法進行頁面控制
  */
 public String execute(AdmitForm form, AdmitActionContext context) throws Exception{
  log.debug("-= start =-");
  
  try {
   if (form == null) {
    throw new Exception("admitForm is null");
   }
   
   //獲得當前Form對象
   LoginForm loginForm = (LoginForm)form;
   
   /*
    * 調用LoginBL的業務處理方法進行處理
    */
   LoginBl bl = new LoginBl(context);
   
   int result = bl.doBussiness(loginForm);
   
   log.debug("-= end =-");
   
   //根據業務處理的結果得到跳轉對象 返回給控制檯 struts-config.xml
   if (result == 0) {
    return "success";
   } else if(result ==1){
    return "error";
   } else {
       return "relogin";
   }
  
   } catch (Exception e) {
   throw e;
  }
  
 } 
 
 /**
  * 檢查登陸
  */
 public boolean CheckLogin(AdmitActionContext context) {
  return true;
 }

}

發佈了0 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章