今天被问到这个问题,就实验了一下,后续会继续补充一些配置项的意思。
一:操作步骤
1.大纲
2.新建sql
1 -- ---------------------------- 2 -- Table structure for user 3 -- ---------------------------- 4 DROP TABLE IF EXISTS `user`; 5 CREATE TABLE `user` ( 6 `id` int(16) NOT NULL, 7 `name` varchar(255) DEFAULT NULL, 8 `password` varchar(255) DEFAULT NULL, 9 `email` varchar(255) DEFAULT NULL, 10 `phone` varchar(255) DEFAULT NULL, 11 PRIMARY KEY (`id`) 12 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 13 14 -- ---------------------------- 15 -- Records of user 16 -- ---------------------------- 17 INSERT INTO `user` VALUES ('1', 'aa', '12', '12@qq.com', '1234567890');
3.domain
1 package com.jun.it; 2 3 public class User { 4 private int id; 5 private String name; 6 private String password; 7 private String email; 8 private String phone; 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public String getPassword() { 22 return password; 23 } 24 public void setPassword(String password) { 25 this.password = password; 26 } 27 public String getEmail() { 28 return email; 29 } 30 public void setEmail(String email) { 31 this.email = email; 32 } 33 public String getPhone() { 34 return phone; 35 } 36 public void setPhone(String phone) { 37 this.phone = phone; 38 } 39 @Override 40 public String toString() { 41 return "User [id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", phone=" + phone 42 + "]"; 43 } 44 45 }
4.c3p0-config.xml
1 <c3p0-config> 2 3 <!-- c3p0默认配置,下面还可以配置多个数据库 --> 4 <default-config> 5 <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3308/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true</property> 6 <property name="driverClass">com.mysql.cj.jdbc.Driver</property> 7 <property name="user">root</property> 8 <property name="password">123456</property> 9 <property name="minPoolSize">5</property> 10 <property name="maxPoolSize">2</property> 11 <property name="maxIdleTime">10</property> 12 </default-config> 13 14 </c3p0-config>
5.测试类
1 package com.jun.it; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import org.junit.Test; 10 11 import com.mchange.v2.c3p0.ComboPooledDataSource; 12 13 public class C3p0Test { 14 //使用编码方式实现c3p0数据库连接池 15 @Test 16 public void TestXml() throws Exception{ 17 //第一步:创建连接池核心工具类 18 ComboPooledDataSource dataSource=new ComboPooledDataSource(); 19 20 //第三步:从连接池对象中获取数据库连接 21 Connection con=dataSource.getConnection(); 22 String sql="select * from user "; 23 PreparedStatement ps=con.prepareStatement(sql); 24 ResultSet rs=ps.executeQuery(); 25 26 List<User> list=new ArrayList<User>(); 27 while(rs.next()){ 28 User user=new User(); 29 user.setId(rs.getInt("id")); 30 user.setName(rs.getString("name")); 31 user.setPassword(rs.getString("password")); 32 user.setEmail(rs.getString("email")); 33 user.setPhone(rs.getString("phone")); 34 list.add(user); 35 } 36 37 System.out.println("~~~"+list); 38 } 39 }
6.效果