目录
一、c3p0
1. 添加依赖
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version> 4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2. 在resources中创建applicationContext.xml文件
3. 在applicationContext.xml中创建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://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
标签解析
- id:自己给该对象命名的唯一标签,dataSource就是我命名的
- class:创建的对象所属类的地址,我创建的是ComboPooledDataSource类的对象,所以就是这个类的地址。如何获取类地址?点这里
- 4个name:数据源参数的名字,根据类的相关变量的set方法命名。
//创建数据源
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//设置连接参数
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("root");
解释:ComboPooledDataSource类的四个变量分别为driverClass,jdbcUrl,user,password,这四个名字的是根据四个set方法setDriverClass,setJdbcUrl,setUser,setPassword,去掉set得到的,且将首字母大写改为小写。即setDriverClass,则name=“driverClass”。
- 4个value:为参数赋值
4.测试是否成功连接上数据库
public void test4() throws Exception{
//创建Spring容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取ComboPooledDataSource类的对象(连接池数据源对象)
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
//获取连接
Connection connection = dataSource.getConnection();
//打印连接
System.out.println(connection);
//连接放回连接池
connection.close();
}
结果
com.mchange.v2.c3p0.impl.NewProxyConnection@4c40b76e
二、druid
1. 添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version> 4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2. 在resources中创建applicationContext.xml文件
3. 在applicationContext.xml中创建druid数据源对象
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
标签解析
- id:自己给该对象命名的唯一标签,dataSource1就是我命名的
- class:创建的对象所属类的地址,我创建的是DruidDataSource类的对象,所以就是这个类的地址。如何获取类地址?点这里
- 4个name:数据源参数的名字,根据类的相关变量的set方法命名。
//创建数据源
DruidDataSource dataSource = new DruidDataSource();
//设置连接参数
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("root");
解释:DruidDataSource 类的四个变量分别为driverClassName,url,username,password,这四个名字的是根据四个set方法setDriverClassName,setUrl,setUsername,setPassword,去掉set得到的,且将首字母大写改为小写。即setDriverClassName,则name=“driverClassName”。(与c3p0的name有所不同)
- 4个value:为参数赋值
4.测试是否成功连接上数据库
public void test5() throws Exception{
//创建Spring容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取DruidDataSource类的对象(连接池数据源对象)
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource1");
//获取连接
Connection connection = dataSource.getConnection();
//打印连接
System.out.println(connection);
//连接放回连接池
connection.close();
}
结果
com.mysql.jdbc.JDBC4Connection@402e37bc