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>
分类:
Spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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搭建本