Spring之c3p0连接池配置和使用

1、导入包:c3p0和mchange包

2、代码实现方式:

复制代码
 1 package helloworld.pools;
 2 
 3 import com.mchange.v2.c3p0.ComboPooledDataSource;
 4 import org.springframework.jdbc.core.JdbcTemplate;
 5 import java.beans.PropertyVetoException;
 6 
 7 /**
 8  * c3p0连接池使用方法-代码
 9  * 导入包:c3p0和mchange包
10  */
11 public class C3p0CodeImpl {
12     public static void main(String[] args) {
13         ComboPooledDataSource dataSource = new ComboPooledDataSource();
14         try {
15             dataSource.setDriverClass("com.mysql.jdbc.Driver");
16             dataSource.setJdbcUrl("jdbc:mysql://10.15.1.200:3306/gxrdb");
17             dataSource.setUser("root");
18             dataSource.setPassword("root");
19         } catch (PropertyVetoException e) {
20             e.printStackTrace();
21         }
22 
23         // 设置数据源
24         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
25 
26 //        调用jdbcTemplate对象中的方法实现操作
27         String sql = "insert into user value(?,?,?)";
28         //表结构:id(int、自增),name(varchar 100),age(int 10)
29         int rows = jdbcTemplate.update(sql, null, "Tom2", 25);
30         System.out.println("插入行数:" + rows);
31     }
32 }
复制代码

3、Spring配置实现方式

beans.xml

复制代码
 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:contexnt="http://www.springframework.org/schema/context"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/context
 7         http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 8 
 9     <!--配置c3p0连接池-->
10     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
11         <!--注入属性-->
12         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
13         <property name="jdbcUrl" value="jdbc:mysql://10.15.1.200:3306/gxrdb"></property>
14         <property name="user" value="root"></property>
15         <property name="password" value="root"></property>
16     </bean>
17 
18 
19 </beans>
复制代码

 

posted @   星瑞  阅读(1235)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示