JavaEE:JDBC连接池用法

    xiaoxiao2022-07-05  142

    说明:

    也叫数据源,实现了DataSoruce接口。

    一、DBCP使用:

    依赖jia包:commons-dbcp2-x.x.x.jar、commons-logging-x.x.jar、commons-pool2-x.x.x.jar

    1.创建DBCP配置文件,在"工程/WebContent/WEB-INF/config/dbcpconfig.properties"中增加:

    #连接设置 driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC username=root password=root #初始化连接数 initialSize=10 #最大连接数 maxActive=50 #最大空闲连接数 maxIdle=20 #最小空闲连接数 minIdle=5 #超时等待时间,毫秒为单位 maxWait=60000 connectionProperties=useUnicode=true;characterEncoding=utf-8 #指定自动提交 defaultAutoCommit=true #指定事务级别 defaultTransactionIsolation=READ_UNCOMMITTED

    2.获取DBCP创建的Connection:

    /** * 获取DBCP创建的Connection */ public Connection getDBCPConnection(ServletContext context) throws Exception { //1.获取DBCP配置文件路经 String fileName = "dbcpconfig.properties"; String filePath = context.getRealPath("WEB-INF/config/" + fileName); //2.加载DBCP配置文件 Properties prop = new Properties(); prop.load(new FileInputStream(filePath)); //3.创建DataSource对象 BasicDataSourceFactory factory = new BasicDataSourceFactory(); DataSource ds = factory.createDataSource(prop);// 得到数据源对象 //4.返回Connection Connection conn = ds.getConnection(); return conn; }

    3.拿到Connection后,进行增删改查操作,和JDBC操作一样。

    二、C3P0使用:

    依赖jia包:c3p0-x.x.x.x.jar、mchange-commons-java-x.x.x.jar

    1.创建C3P0配置文件,在"工程/src/c3p0-config.xml"中增加:

    <c3p0-config> <default-config> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </default-config> </c3p0-config>

    2.获取C3P0创建的Connection:

    /** * 获取C3P0创建的Connection */ public Connection getC3P0Connection(ServletContext context) throws Exception { // 1.自动加载DBCP配置文件c3p0-config.xml,此文件要放在src目录下,默认会找xml中的default-config节点 ComboPooledDataSource ds = new ComboPooledDataSource(); // 2.返回Connection Connection conn = ds.getConnection(); return conn; }

    3.拿到Connection后,进行增删改查操作,和JDBC操作一样。

    三、Tomcat的连接池: 1.此种配置下,驱动jar文件需放置在tomcat的lib下: 在WebRoot/META-INF/建军一个context.xml文件,写如下代码:

    <Context> <Resource name="jdbc/任意名称a"  //为了让程序可以拿到这个配置文件 auth="Container" type="javax.sql.DataSource" username="数据库用户名" password="数据库密码" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/数据库名" maxActive="8" maxIdle="4"/> </Context>

    2.用JNDI(Java Naming and Directory Interface)技术拿到Tomcat的连接池: 在javax.naming包下,其核心API为Context,它代表JNDI容器,其lookup方法为检索容器中对应名称的对象。

    // 1.JNDI初始化 Context initCtx = new InitialContext(); //jndi初始化 Context envCtx = (Context) initCtx.lookup("java:comp/env");//得到tomcat中的JNDI容器 // 2.获取DataSource,context.xml中配置的数据库连接池 DataSource ds = (DataSource) envCtx.lookup("jdbc/任意名称a"); // 3.返回Connection ds.getConnection();

    3.拿到Connection后,进行增删改查操作,和JDBC操作一样。

    最新回复(0)