Java基础之 连接池 dbcp c3p0
来源:互联网 发布:北师大网络信息门户 编辑:程序博客网 时间:2024/06/05 22:58
dbcp连接池
//dbcp连接池--直接读取dbcp.properties配置文件public Connection dbcpDataSourceByFactory() throws Exception{//1.从配置文件中读取配置参数Properties properties = new Properties();InputStream in = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");properties.load(in);//2.从BasicDataSourceFactory中获取BasicDataSource dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);//3.从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}//dbcp连接池--直接设置参数public Connection dbcpDataSource() throws SQLException{BasicDataSource dataSource = null;//1.创建dbcp数据源实例dataSource = new BasicDataSource();//2.设置必需的参数dataSource.setUsername("root");dataSource.setPassword("123");dataSource.setUrl("jdbc:mysql:\\localhost:3316\test");dataSource.setDriverClassName("com.mysql.jdcb.Driver");//3.设置配置参数//1) 数据库连接池中,初始化连接数的个数dataSource.setInitialSize(5);//2) 最大连接数dataSource.setMaxActive(10);//3) 最小连接数dataSource.setMinIdle(5);//4) 等待数据库连接池分配连接的最长时间dataSource.setMaxActive(10);//从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}
dbcp.properties
username=rootpassword=1230driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///testinitialSize=10maxActive=50minIdle=5maxWait=5000
c3p0链接池
//c3p0链接池 -- 读取c3p0-config.xml 配置文件 public Connection c3p0DataSourceByXml() throws Exception { //1.通过c3p0-config.xml 创建 ComboPooledDataSource 实例 ComboPooledDataSource cpds = new ComboPooledDataSource("hdc3p0"); //2.从数据库连接池中获取链接 Connection conn = cpds.getConnection(); return conn; } //c3p0链接池 --- 直接设置参数 public Connection c3p0DataSource() throws Exception { //1.创建 ComboPooledDataSource 实例 ComboPooledDataSource cpds = new ComboPooledDataSource(); //2.设置参数 cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql:///atguigu"); cpds.setUser("root"); cpds.setPassword("1230"); //3.从数据库连接池中获取链接 Connection conn = cpds.getConnection(); return conn; }
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?><c3p0-config><named-config name="hdc3p0"><!-- 指定连接数据源的基本属性 --><property name="user">root</property><property name="password">1230</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///atguigu</property><!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 --><property name="acquireIncrement">5</property><!-- 初始化数据库连接池时连接的数量 --><property name="initialPoolSize">5</property><!-- 数据库连接池中的最小的数据库连接数 --><property name="minPoolSize">5</property><!-- 数据库连接池中的最大的数据库连接数 --><property name="maxPoolSize">10</property><!-- C3P0 数据库连接池可以维护的 Statement 的个数 --><property name="maxStatements">20</property><!-- 每个连接同时可以使用的 Statement 对象的个数 --><property name="maxStatementsPerConnection">5</property></named-config></c3p0-config>
dbcp连接池
//dbcp连接池--直接读取dbcp.properties配置文件public Connection dbcpDataSourceByFactory() throws Exception{//1.从配置文件中读取配置参数Properties properties = new Properties();InputStream in = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");properties.load(in);//2.从BasicDataSourceFactory中获取BasicDataSource dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);//3.从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}//dbcp连接池--直接设置参数public Connection dbcpDataSource() throws SQLException{BasicDataSource dataSource = null;//1.创建dbcp数据源实例dataSource = new BasicDataSource();//2.设置必需的参数dataSource.setUsername("root");dataSource.setPassword("123");dataSource.setUrl("jdbc:mysql:\\localhost:3316\test");dataSource.setDriverClassName("com.mysql.jdcb.Driver");//3.设置配置参数//1) 数据库连接池中,初始化连接数的个数dataSource.setInitialSize(5);//2) 最大连接数dataSource.setMaxActive(10);//3) 最小连接数dataSource.setMinIdle(5);//4) 等待数据库连接池分配连接的最长时间dataSource.setMaxActive(10);//从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}
dbcp.properties
username=rootpassword=1230driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///testinitialSize=10maxActive=50minIdle=5maxWait=5000
c3p0链接池
//c3p0链接池 -- 读取c3p0-config.xml 配置文件 public Connection c3p0DataSourceByXml() throws Exception { //1.通过c3p0-config.xml 创建 ComboPooledDataSource 实例 ComboPooledDataSource cpds = new ComboPooledDataSource("hdc3p0"); //2.从数据库连接池中获取链接 Connection conn = cpds.getConnection(); return conn; } //c3p0链接池 --- 直接设置参数 public Connection c3p0DataSource() throws Exception { //1.创建 ComboPooledDataSource 实例 ComboPooledDataSource cpds = new ComboPooledDataSource(); //2.设置参数 cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql:///atguigu"); cpds.setUser("root"); cpds.setPassword("1230"); //3.从数据库连接池中获取链接 Connection conn = cpds.getConnection(); return conn; }
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?><c3p0-config><named-config name="hdc3p0"><!-- 指定连接数据源的基本属性 --><property name="user">root</property><property name="password">1230</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///atguigu</property><!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 --><property name="acquireIncrement">5</property><!-- 初始化数据库连接池时连接的数量 --><property name="initialPoolSize">5</property><!-- 数据库连接池中的最小的数据库连接数 --><property name="minPoolSize">5</property><!-- 数据库连接池中的最大的数据库连接数 --><property name="maxPoolSize">10</property><!-- C3P0 数据库连接池可以维护的 Statement 的个数 --><property name="maxStatements">20</property><!-- 每个连接同时可以使用的 Statement 对象的个数 --><property name="maxStatementsPerConnection">5</property></named-config></c3p0-config>
0 0
- Java基础之 连接池 dbcp c3p0
- java数据连接池 c3p0 dbcp proxool
- java—连接池 C3p0 DBCP
- Java连接池优化 DBCP/C3P0
- 连接池-DBCP-C3P0
- 连接池-C3P0、DBCP
- C3P0和DBCP连接池
- DBCP与C3P0连接池
- 连接池 DBCP和 C3P0
- JDBC连接池 DBCP C3P0
- JAVA 入门学习之路-数据源/连接池-DBCP和C3P0。
- JDBC之连接池DBCP与c3p0的简单使用
- java 各个连接池学习总结(DBCP C3P0 PROXOOL)
- java工程连接数据库(数据源dbcp/c3p0)
- DBCP连接池和C3P0连接池
- DBCP和C3P0连接池常用配置
- dbcp和c3p0数据连接池比较
- DBCP和C3P0连接池配置介绍
- Android关于微博发表微博时@好友后删除@的好友的功能实现
- android studio 中获取MD5和SHA1值
- [产品经理]产品管理的工作流程
- Ubuntu下Samba服务器的安装和简单使用
- 强大灵活的Sublime3正则替换功能
- Java基础之 连接池 dbcp c3p0
- Android中的Cookies和WebView缓存
- 刷题记录
- ACM——划水ing——背包讨论
- 垃圾回收机制
- 笔记
- 子网掩码及其作用
- NetApp FAS 系列Linux DM安装步骤
- 1002.Alphacode