1.数据和连接池操作类
/**
* 数据库连接池
* @author huxu
*
*/
public class DBPool {
private static DBPool dbPool;
private ComboPooledDataSource dataSource;
final static private String url = Config.DBURL;
final static private String username = Config.DBUSER;
final static private String password = Config.DBPWD;
final static private String driver = Config.DBDRIVER;
static {
dbPool = new DBPool();
}
public DBPool() {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setJdbcUrl(url);
dataSource.setDriverClass(driver);
dataSource.setInitialPoolSize(5);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(30);
dataSource.setMaxStatements(0);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public final static DBPool getInstance() {
return dbPool;
}
public void close() {
dataSource.close();
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("oracle funtimeException", e);
}
}
public static void main(String[] args) throws SQLException {
Connection conn = null;
try {
conn = DBPool.getInstance().getConnection();
System.out.println("conn******* " + conn);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
import java.sql.Connection;
public abstract class Handle {
protected Connection conn = null;
public Connection getConnection() {
conn = DBPool.getInstance().getConnection();
return conn;
}
}
public class DbHandle extends Handle {
public static DbHandle instance;
private DbHandle() {
}
public static DbHandle getInstance() {
if (instance == null) {
instance = new DbHandle();
}
return instance;
}
增、删、改、查处理
}
2.项目配置类
public class Config {
public static final String DBURL = ResourceBundleImp.getValue("url");//DBURL
public static final String DBUSER = ResourceBundleImp.getValue("username");//用户名
public static final String DBPWD = ResourceBundleImp.getValue("password");//密码
/**
* 读取配置文件
* @author huxu
*
*/
public class ResourceBundleImp {
private static ResourceBundle resourceBundle = null;
/**
* 获得关键字的值
* @param key String 关键字
* @return String 值
*/
public static String getValue(String key) {
if (resourceBundle == null) {
instantiation();
}
return resourceBundle.getString(key);
}
/**
* 实例化配置文件
*/
public static void instantiation() {
//smsOrder_xzk smsOrder_cz1 smsOrder_cz2
resourceBundle = ResourceBundle.getBundle("config");
}
}
JDBC技术学习 https://www.itkc8.com