//DB连接池类,扩展了插件接口
public class DbConPools implements PlugIn {
private static Log log = LogFactory.getLog(DbConPools.class);
//定义属性文件(在SRC下创建mydb.properties属性文件)
private static String propertiesFile = "mydb";
//定义连接池数据源
private static SharedPoolDataSource dataSource = null;
//初始化数据源
public void init(ActionServlet servlet, ModuleConfig config) throws ServletException {
try {
//获得属性文件的内容
ResourceBundle bundle = ResourceBundle.getBundle(propertiesFile);
//设置具体信息
String driver = bundle.getString("driver.name");
String schemaUri = bundle.getString("jdbc.url");
String schemaUser = bundle.getString("jdbc.user");
String schemaPass = bundle.getString("jdbc.pass");
String schemaMaxActive = bundle.getString("max.active");
String schemaMaxWait = bundle.getString("max.wait");
//commons-dbcp-1.2.1.jar包导入需要。防止数据库连接池泄漏,用来跟踪和恢复这些遗弃的数据库连接。
DriverAdapterCPDS cpds = new DriverAdapterCPDS();
cpds.setDriver(driver);
cpds.setUrl(schemaUri);
cpds.setUser(schemaUser);
cpds.setPassword(schemaPass);
//创建数据源并设定相关参数
dataSource = new SharedPoolDataSource();
dataSource.setConnectionPoolDataSource(cpds);
dataSource.setMaxActive(Integer.parseInt((schemaMaxActive)));
dataSource.setMaxWait(Integer.parseInt((schemaMaxWait)));
} catch (Exception e) {
throw new ServletException(e);
}
}
//获得一个DB连接
public static Connection getConnection() throws Exception {
Connection conn = dataSource.getConnection();
if (conn == null) {
throw new SQLException("XXXX-getConnection");
}
//将数据库自动提交设定为不可用
conn.setAutoCommit(false);
return conn;
}
//销毁数据源
public void destroy() {
try {
if (dataSource != null && (dataSource.getNumActive() + dataSource.getNumIdle()) != 0) {
log.info("datasource close. ");
dataSource.close();
}
} catch (Exception e) {
log.warn("XXXX-destroy"。, e);
}
}
}
SRC下mydb.properties属性文件内容:
driver.name=org.postgresql.Driver
jdbc.url=jdbc:postgresql://192.168.0.1:5432/TESTDB
jdbc.user=user01
jdbc.pass=user01
max.active=150
max.wait=500000
在struts-config.xml中添加这个插件
<plug-in className="XXXX.XXXX.common.db.DbConPools"/>
下面再贴出一个数据操作类仅供参考
public class CustomQueryRunner extends QueryRunner {
private PreparedStatement staticStatement = null;
public CustomQueryRunner() {
super();
}
public CustomQueryRunner(DataSource ds) {
super(ds);
}
protected void fillStatement(PreparedStatement statement, Object[] params) throws SQLException {
if (params == null) {
return;
}
for (int i = 0; i < params.length; i++) {
if (params[i] == null) {
statement.setNull(i + 1, Types.VARCHAR);
} else if (params[i] instanceof String) {
statement.setString(i + 1, (String) params[i]);
} else if (params[i] instanceof Date) {
statement.setTimestamp(i + 1, new Timestamp(((Date) params[i]).getTime()));
} else if (params[i] instanceof Integer) {
statement.setInt(i + 1, ((Integer) params[i]).intValue());
} else if (params[i] instanceof Long) {
statement.setLong(i + 1, ((Long) params[i]).longValue());
} else if (params[i] instanceof Double) {
statement.setDouble(i + 1, ((Double) params[i]).doubleValue());
} else if (params[i] instanceof Float) {
statement.setFloat(i + 1, ((Float) params[i]).floatValue());
} else if (params[i] instanceof Boolean) {
statement.setBoolean(i + 1, ((Boolean) params[i]).booleanValue());
} else {
statement.setObject(i + 1, params[i]);
}
}
}
protected PreparedStatement prepareStatement(Connection conn, String sql) throws SQLException {
if (staticStatement == null) {
staticStatement =
conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
return staticStatement;
}
public Object query(Connection conn, String sql, Object[] params, ResultSetHandler rsh) throws SQLException {
PreparedStatement stmt = null;
ResultSet rs = null;
Object result = null;
try {
stmt = this.prepareStatement(conn, sql);
this.fillStatement(stmt, params);
rs = this.wrap(stmt.executeQuery());
result = rsh.handle(rs);
} catch (SQLException e) {
this.rethrow(e, sql, params);
} finally {
try {
close(rs);
} finally {
close(stmt);
}
}
return result;
}
public Object query(Connection conn, String sql, List params, ResultSetHandler rsh) throws SQLException {
return query(conn, sql, params.toArray(), rsh);
}
public Object query(String sql, List params, ResultSetHandler rsh) throws SQLException {
return query(sql, params.toArray(), rsh);
}
public int update(Connection conn, String sql, List params) throws SQLException {
return update(conn, sql, params.toArray());
}
public int update(String sql, List params) throws SQLException {
return update(sql, params.toArray());
}
}