代码改变世界

Java c3p0连接池

2016-12-26 20:39  甘雨路  阅读(205)  评论(0编辑  收藏  举报
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.ConnectionPoolDataSource;
import javax.swing.text.DefaultEditorKit.InsertBreakAction;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ConnectData {
    
    public static void main(String[] args) {
        
        System.out.println(ConnectData.getConnection());
    }
    
    private static String userName="LF";
    private static String password="LF";
    public static Connection getConnection() {
        //创建连接池
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //通过映射来驱动
        try {
            dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        //设置url
        dataSource.setJdbcUrl("jdbc:oracle:thin:@192.168.10.105:1521:orcl");
        //设置数据库用户账号
        dataSource.setUser(userName);
        //设置数据库密码
        dataSource.setPassword(password);
        dataSource.setInitialPoolSize(5);
        dataSource.setMaxPoolSize(13);
        dataSource.setMinPoolSize(3);
        try {
            // 获取连接
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}