**需要下载jar包:
如果jar包版本是c3p0——0.9.5.1(包含0.9.5.1)以前的,只需一个jar包就可以,
如果jar包版本是c3p0——0.9.5.2(包含0.9.5.2)之后的,另需一个mchange.jar包
接下来,新建配置文件: c3p0-config.xml
c3p0-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 这是默认配置信息 -->
<default-config name="mysqlname">
<!-- 连接四大参数配置 -->
<property name="jdbcUrl">jdbc:mysql://211.68.191.30:3306/flag_test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">Knorray101326#!</property>
<!-- 池参数配置 -->
<property name="acquireIncrement">10</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">120</property>
<property name="minPoolSize">40</property>
<property name="maxPoolSize">10</property>
</default-config>
</c3p0-config>
然后是JDBCUtil.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil {
private static DataSource ds = new ComboPooledDataSource("mysqlname");
// 获取连接池
public static DataSource getDataSource() {
return ds;
}
// 获取连接
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 关闭连接
* @param resultSet
* @param pst
* @param connection
*/
public static void closeConnection(ResultSet resultSet, PreparedStatement pst, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
//测试方法
public static void main(String[] args) {
//JDBCUtil dbUtil = new JDBCUtil();
Connection connection;
try {
connection = JDBCUtil.getConnection();
if (connection != null) {
System.out.println("连接成功");
} else {
System.out.println("连接失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
数据库操作(DAO层):
//测试c3p0 , 插入方法
public boolean insert(Student stu) throws Exception {
String sql = "insert into student(yb_userid,yb_username,yb_college, yb_schoolname) VALUES(?,?,?,?);";
Connection connection = jdbcUtil.getConnection();
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, stu.getYb_userid());
pstmt.setString(2, stu.getYb_username());
pstmt.setString(3, stu.getYb_college());
pstmt.setString(4, stu.getYb_schoolname());
int count = pstmt.executeUpdate();
System.out.println("flag插入用户:" + stu.getYb_username()+ "count======" + count);
jdbcUtil.closeConnection(null , pstmt, connection);
return count > 0 ? true : false;
}
Servlet层;
//测试插入
private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
StudentDao studentDao = new StudentDao();
String yb_userid = "101";
String yb_username = "梁振辉";
String yb_college = "信息科学与技术学院";
String yb_schoolname = "农大";
int status = 0;
int visit = 0;
Student student = new Student();
student.setYb_userid(yb_userid);
student.setYb_username(yb_username);
student.setYb_college(yb_college);
student.setYb_schoolname(yb_schoolname);
student.setStatus(status);
student.setVisit(visit);
try {
studentDao.insert(student);
} catch (Exception e) {
e.printStackTrace();
}
}