数据源公共类:
package com.montnets.emp.netnews.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.montnets.emp.netnews.bean.StaticValue;
import org.apache.log4j.Logger;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class DataSourceUtil {
private final static Logger logger = Logger.getLogger(DataSourceUtil.class);
private static DataSource ds;
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void init() {
try {
if (ds == null) {
ResourceBundle rb = ResourceBundle.getBundle("resourceBundle");
String ip = rb.getString("db.ip");
String port = rb.getString("db.port");
String dbname = rb.getString("db.dbname");
String user = rb.getString("db.user");
String password = rb.getString("db.password");
String dbType = rb.getString("db.type");
int initSize = Integer.parseInt(rb.getString("db.initsize"));
DruidDataSource dataSource = new DruidDataSource();
if ("mysql".equals(dbType)) {
String jdbcURL = new StringBuilder().append("jdbc:mysql://")
.append(ip)
.append(":")
.append(port)
.append("/")
.append(dbname)
.toString();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(jdbcURL);
dataSource.setPoolPreparedStatements(false);
} else {
String jdbcURL = new StringBuilder().append("jdbc:oracle:thin:@")
.append(ip)
.append(":")
.append(port)
.append(":")
.append(dbname)
.toString();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl(jdbcURL);
dataSource.setValidationQuery("select 1 from dual");
dataSource.setPoolPreparedStatements(