数据库
/*
* 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;
}
}