javaEE---dbcp和c3p0连接数据库

c3p0配置文件

 

c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="jdbcUrl">jdbc:sqlserver://localhost:1433;DatabaseName=bookStore</property>
        <property name="user">Dzg</property>
        <property name="password">Dong</property>
        <!-- 初始化连接池数量 -->
        <property name="initialPoolSize">10</property>

        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime">60</property>

        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">25</property>

        <!--连接池中保留的 最小连接数,Default:3 -->
        <property name="minPoolSize">10</property>
    </default-config>
</c3p0-config>

 

public class JDBCUtil {

 

 

 

private static DataSource dataSource=null;

 

static{

 

dataSource=new ComboPooledDataSource("mysql");

 

}

 

 

 

/**

 

* 获取数据库连接

 

* @return

 

*/

 

public static Connection getConnection(){

 

Connection conn=null;

 

try {

 

conn=dataSource.getConnection();

 

} catch (SQLException e) {

 

e.printStackTrace();

 

}

 

return conn;

 

}

 

 

 

 

 

/**

 

* 关闭数据库连接

 

* @param conn

 

*/

 

public static void closeConn(Connection conn){

 

try {

 

if(conn!=null && conn.isClosed()){

 

conn.close();

 

}

 

} catch (SQLException e) {

 

e.printStackTrace();

 

}

 

}

 

}

dbcp配置文件 

dbcpconfig.properties:
driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;DatabaseName=struts
username=数据库用户名
password=密码
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=READ_COMMITTED

工具类DBCPUtil :

package com.dong.structs.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

/**
 * 使用开源数据源:
 *     DBCP:DataBase Connection Pool 是apache公司实现的一个开源的数据源。
 * 使用步骤:
 *     1.拷贝jar包
 *  2.写配置文件
 *  3.使用
 *
 */
public class DBCPUtil {
  
   //1.定义一个数据源
   private static DataSource ds;
   
   //2.使用静态代码块,给数据源赋值
   static{
      try {
         Properties prop = new Properties();
         InputStream in = DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
         prop.load(in);
         ds = BasicDataSourceFactory.createDataSource(prop);
      } catch (Exception e) {
         throw new ExceptionInInitializerError("初始化连接池失败!");
      }
   }
   
   
   
   //3.提供一个获取数据源的方法
   public static DataSource getDataSource(){
      return ds;
   }
   
   //4.提供一个获取连接的方法,注意,以后获取连接,必须使用该方法
   public static Connection getConnection(){
      try {
         return ds.getConnection();
      } catch (SQLException e) {
         throw new RuntimeException(e);
      }
   }
}
调用:
QueryRunner queryRunner=new QueryRunner(DBCPUtil.getDataSource());
String sql="insert into [user]  (  username , password , birth , hody , modify  ) values  (  ? , ? , ? , ? , ?  )";
try {
    queryRunner.update(sql,user.getUsername(),user.getPassword(),user.getBirth(),user.getHody(),user.getModify());
} catch (SQLException e) {
    e.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值