一 使用步骤:
1、添加jar包
2、编写配置文件
c3p0-config.xml,放在classpath中,或classes目录中(javeee 中的src中)
二 配置文件的写法
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day06</property>
<property name="user">root</property>
<property name="password">abc</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
</c3p0-config>
三、编写工具类:
C3P0Util.java
package com.it.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
//注意包一定要导对
public class C3P0Util {
//得到一个数据源
private static DataSource dataSource = new ComboPooledDataSource();
//从数据源中得到一个连接对象
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("服务器错误");
}
}
public static void release(Connection conn,Statement stmt,ResultSet rs){
//关闭资源
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();//关闭
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}
}
UserDao.java
package com.it.dao;
import java.util.List;
import com.ithema.domain.User;
public interface UserDao {
public List<User> findUsers(String sql);
}
UserDaoImpl.java
package com.it.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.it.dao.UserDao;
import com.it.domain.User;
import com.it.util.C3P0Util;
public class UserDaoImpl implements UserDao {
public List<User> findUsers(String sql) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<User> list=new ArrayList<User>();
conn=C3P0Util.getConnection();
try {
ps= conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
User user=new User();
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
user.setEmail(rs.getString(4));
user.setBirthday(rs.getDate(5) );
list.add(user);
}
return list;
} catch (SQLException e) {
throw new RuntimeException("服务器错误");
}
}
}
user.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.it.domain.User"%>
<%@page import="com.it.dao.impl.UserDaoImpl"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
UserDaoImpl dao=new UserDaoImpl();
List<User> list;
list=dao.findUsers("select * from users");
request.setAttribute("list", list);
%>
<table border="1">
<tr>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>生日</th>
</tr>
<c:forEach var="i" begin="1" end="3">
<tr>
<td>${list[i].name }</td>
<td>${list[i].password }</td>
<td>${list[i].email }</td>
<td>${list[i].birthday }</td>
</tr>
</c:forEach>
<c:forEach var="l" items="${list }">
<tr>
<td>${l.name }</td>
<td>${l.password }</td>
<td>${l.email }</td>
<td>${l.birthday }</td>
</tr>
</c:forEach>
</table>
</body>
</html>