JDBC几种常用的连接池
JDBC几种常用的连接池
在做Java项目的时候,一直都是同事做这些项目的配置,因为是半路出家,当时是项目缺人,就开始跟着做了Java Web.一直也没有好好学学基础知识.以后对比C#去学习基础知识.谁让它两是近亲呢.
2. C3P0连接池
3. DBCP连接池
在项目中一直使用C3P0连接池,这里顺便了解其他几种.
Spring JdbcTemplate连接池
先采用代码的方法,创建一个表,做测试. 当忘记在配置文件属性的,可以在代码查找.package qiufeng.spring.jdbctemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:Jdbc.xml")
public class JdbcTemplateTest {
@Test
public void createTableTest() {
//1. 创建数据库的连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.01:3306/UserInfo");
dataSource.setUsername("root");
dataSource.setPassword("123");
//2. 通过连接池构造模板对象
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table User(id int primary key,name varchar(16));");
}
}
使用配置方式使用
xml配置<!-- 使用spring 内置连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///UserInfo"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
使用注入测试连接是否正常,下面两种的测试也用同样的代码.
package qiufeng.spring.jdbctemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:Jdbc.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void springTestJdbc() {
jdbcTemplate.execute("create table User1(id int primary key,name varchar(16));");
}
}
C3P0连接池
<!-- c3p0 链接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///UserInfo"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
DBCP连接池
<!-- 使用dbcp 内置连接池 -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///UserInfo"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
秋风
2017-08-23