dbsqlauthbase.java
来自「一个用struts tiles的在线影院web系统」· Java 代码 · 共 125 行
JAVA
125 行
package com.eline.vod.security.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import com.blue.web.common.exceptions.DAOSysException;
import com.blue.web.common.jdbc.DBManager;
import com.blue.web.common.util.AppLogger;
import com.blue.web.common.util.AppSettings;
import com.eline.vod.security.configuration.ConfigurationSettings;
import com.eline.vod.utils.AppKeys;
public class DBSqlAuthBase implements DBManager {
protected static final String MIN_DATE = "0001-01-01T00:00:00";
protected static final String MAX_DATE = "9999-12-31T23:59:59";
protected DataSource _datasource = null;
//
// DBSqlManager Override methods
//
public DBSqlAuthBase() throws DAOSysException {
try {
InitialContext ic = new InitialContext();
AppSettings config = AppSettings.getInstance();
ConfigurationSettings authConfig = ConfigurationSettings.getInstance();
if (config.getProperty(AppKeys.APP_SERVER) != null
&& config.getProperty(AppKeys.APP_SERVER).equalsIgnoreCase("jboss")) {
_datasource = (DataSource) ic.lookup("java:/" + authConfig.getDataSource());
} else {
Context envCtx = (Context)ic.lookup("java:/comp/env");
_datasource = (DataSource) envCtx.lookup(authConfig.getDataSource());
}
} catch (NamingException e) {
System.out.println("Failure to initialize authentication datasource : " + e.getMessage() + "\n");
} catch (Exception e) {
throw new DAOSysException("General exception : " + e.getMessage() + "\n");
}
}
public Connection getDBConnection() throws DAOSysException {
try {
return _datasource.getConnection();
} catch (Exception e) {
AppLogger.warn("can not get db datasource, try to use debug connection.");
try {
return getDebugDBConnection();
} catch (Exception e1) {
AppLogger.fatal("failure to get authentication datasource connection.");
throw new DAOSysException(e1.getMessage());
}
}
}
private Connection getDebugDBConnection() throws DAOSysException {
Connection conn = null;
ConfigurationSettings authConfig = ConfigurationSettings.getInstance();
String url = authConfig.getProperty("dbUrl");
String user = authConfig.getProperty("dbUser");
String password = authConfig.getProperty("dbPassword");
try {
// 也许微软的SQL Server JDBC驱动有BUG, 获取Column只能从左至右.
// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}
/**
* 关闭数据库连接
*/
public void closeConnection(Connection conn) throws DAOSysException {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
throw new DAOSysException(e.getMessage());
}
}
/**
*
* @param rst
* @throws SQLException
*/
public void closeResultSet(ResultSet rst) throws DAOSysException {
try {
if (rst != null)
rst.close();
} catch (SQLException e) {
throw new DAOSysException(e.getMessage());
}
}
/**
*
* @param stat
* @throws SQLException
*/
public void closeStatement(PreparedStatement stat) throws DAOSysException {
try {
if (stat != null)
stat.close();
} catch (SQLException e) {
throw new DAOSysException(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?