jdbc连接池

来源:互联网 发布:说说大数据的来源 编辑:程序博客网 时间:2024/05/16 07:57

<p><span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap;">本文章主要介绍手动创建DBCP连接池,C3P0连接池和tomcat下自动创建DBCP连接池</span></p><p><span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap;">DBCP连接池:</span></p><p><span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap;">通过读取properties配置文件,进行设置连接池</span></p><p><span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre;">#连接设置</span></p><p><span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap;"><span style="white-space: pre;">driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/day02username=rootpassword=root#<!--初始化连接-->initialSize=10#<!--最大连接数量-->maxActive=50#<!--最大空闲连接-->maxIdle=20#<!--最小空闲连接-->minIdle=5</span></span></p>
private static Connection conn = null;private static PreparedStatement ps = null;private static ResultSet rs = null;private static DataSource ds = null;static{Class clazz = Jdbc_Dbcp.class;//加载类ClassLoader cl = clazz.getClassLoader();//返回的类的类加载器InputStream input = cl.getResourceAsStream("dbcpconfig.properties");///通过类的类加载器获得一个输入流try {Properties prop = new Properties();prop.load(input);//装载properties文件的模板代码ds = BasicDataSourceFactory.createDataSource(prop);//通过工程类创建一个DataSource} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}
<span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap; background-color: rgb(240, 240, 240);">C3P0连接池 <span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap; background-color: rgb(240, 240, 240);">通过读取</span></span><span style="line-height: 29.7000007629395px; font-family: 'microsoft yahei'; white-space: pre-wrap;">c3p0-config.xml</span><span style="font-family: 'microsoft yahei'; line-height: 29.7000007629395px; white-space: pre-wrap;"><span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap; background-color: rgb(240, 240, 240);">配置文件,进行设置连接池</span></span>
<span style="font-family: 'microsoft yahei'; line-height: 29.7000007629395px; white-space: pre-wrap;"><span style="color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7000007629395px; white-space: pre-wrap; background-color: rgb(240, 240, 240);"><?xml version="1.0" encoding="UTF-8"?><c3p0-config>      <default-config>          <property name="driverClass">com.mysql.jdbc.Driver</property>          <property name="jdbcUrl">jdbc:mysql://localhost:3306/day02</property>          <property name="user">root</property>          <property name="password">root</property>                    <property name="initialPoolSize">10</property>          <property name="maxIdleTime">30</property>          <property name="maxPoolSize">20</property>          <property name="minPoolSize">5</property>          <property name="maxStatements">200</property>      </default-config>            <named-config name="mysql">      <span style="white-space:pre"></span><property name="driverClass">com.mysql.jdbc.Driver</property>          <property name="jdbcUrl">jdbc:mysql://localhost:3306/day02</property>          <property name="user">root</property>          <property name="password">root</property>                  <property name="acquireIncrement">50</property>          <property name="initialPoolSize">100</property>          <property name="minPoolSize">50</property>          <property name="maxPoolSize">1000</property>        <property name="maxStatements">0</property>          <property name="maxStatementsPerConnection">5</property>      </named-config>            <named-config name="oracle">      <span style="white-space:pre"></span><property name="driverClass">com.mysql.jdbc.Driver</property>          <property name="jdbcUrl"></property>          <property name="user">root</property>          <property name="password">root</property>                  <property name="acquireIncrement">50</property>          <property name="initialPoolSize">100</property>          <property name="minPoolSize">50</property>          <property name="maxPoolSize">1000</property>        <property name="maxStatements">0</property>          <property name="maxStatementsPerConnection">5</property>      </named-config>  </c3p0-config>  </span></span>
private static ComboPooledDataSource datasource = new ComboPooledDataSource();//默认的配置<span style="white-space:pre"></span>//初始化的时候可以选择配置<span style="white-space:pre"></span>//ComboPooledDataSource datasource = new ComboPooledDataSource("mysql");<span style="white-space:pre"></span>//ComboPooledDataSource datasource = new ComboPooledDataSource("oracle");<span style="white-space:pre"></span><span style="white-space:pre"></span>/**<span style="white-space:pre"></span> * 获得连接<span style="white-space:pre"></span> * @return<span style="white-space:pre"></span> * @throws SQLException<span style="white-space:pre"></span> */<span style="white-space:pre"></span>public static Connection getConnection() throws SQLException{<span style="white-space:pre"></span>return datasource.getConnection();<span style="white-space:pre"></span>}
tomcat DBCP
通过配置tomcat下的context.xml配置和web应用下的web.xml进行配置,mysql驱动要放在tomcat下的lib文件夹中<pre name="code" class="html"><pre name="code" class="java" style="color: rgb(90, 90, 90); font-size: 18px; line-height: 29.7000007629395px;">context.html下的配置:

<Resource name="jdbc/day02"            auth="Container"            type="javax.sql.DataSource"            username="root"            password="root"            driverClassName="com.mysql.jdbc.Driver"            url="jdbc:mysql://localhost:3306/day02"            maxTotal="8"            maxIdle="4"/>
 
web.xml下的配置:
 <resource-ref>   <res-ref-name>jdbc/day02</res-ref-name>   <res-type>javax.sql.DataSource </res-type>   <res-auth>Container</res-auth>  </resource-ref>
private static DataSource ds = null;<span style="white-space:pre"></span>static {<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>Context initCtx = new InitialContext();//初始化jndi<span style="white-space:pre"></span>Context envCtx = (Context) initCtx.lookup("java:comp/env");//得到jndi容器<span style="white-space:pre"></span>ds = (DataSource) envCtx.lookup("jdbc/day02");//从jndi容器中查找连接池<span style="white-space:pre"></span>} catch (Exception e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}


0 0