数据库与连接池组合

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

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值