c3p0-config.xml
10
30
100
10
200
com.mysql.jdbc.Driver
jdbc:mysql://192.168.111.131:3306/mybatisdb
root
root
10
30
100
10
200
com.mysql.jdbc.Driver
jdbc:mysql://192.168.111.131:3306/mybatisdb
root
root
10
30
100
10
200
mydb.properties
mysqldriver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.111.131:3306/mybatisdb
user=root
password=root
initNum=101
min=31
max=151
increase=32
pom.xml
4.0.0
nj.zb.cn.kgc
jdbcpooldemo
1.0-SNAPSHOT
jdbcpooldemo
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
com.mchange
c3p0
0.9.5.4
mysql
mysql-connector-java
5.1.25
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
maven-site-plugin
3.7.1
maven-project-info-reports-plugin
3.0.0
C3p0Utils
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class C3p0Utils {
static ComboPooledDataSource source = new ComboPooledDataSource("linux01mysql");
public static Connection getConnection() {
Connection connection = null;
try {
connection = source.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection, PreparedStatement pstmt, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
Connection connection = C3p0Utils.getConnection();
System.out.println(connection);
}
}
}
Student
public class Student {
private Integer id;
private String name;
public Student() {
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
StudentDao
import java.util.List;
public interface StudentDao {
public void insertStudent(Liststudents);
}
StudentDaoImpl
import nj.zb.cn.kgc.mypool.MyPoolUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
@Override
public void insertStudent(List students) {
// insert into Student(id,name) values(1,'zs'),(2,'ls'),(3,'ww')
String sql = "insert into Student(id,name) values";
for (Student stu :
students) {
sql += "(" +stu.getId()+","+stu.getName()+ "),";
}
// insert into Student(id,name) values(1,'zs'),(2,'ls'),(3,'ww'),
sql = sql.substring(0,sql.length()-1);
Connection conn = C3p0Utils.getConnection();
// Connection conn = MyPoolUtils.getConnection();
try {
PreparedStatement prest = conn.prepareStatement(sql);
System.out.println(sql);
// int i = prest.executeUpdate();
C3p0Utils.close(conn,prest,null);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List list = new ArrayList<>();
Student zs = new Student(1, "zs");
Student ls = new Student(2, "ls");
list.add(zs);
list.add(ls);
StudentDao studentDao = new StudentDaoImpl();
studentDao.insertStudent(list);
}
}