文章目录
- 什么是C3P0
- 项目目录
- Students.java
- C3P0Conn.java
- StuDao.java
- 套路
- 代码
什么是C3P0
C3P0连接池要比jdbc更简单,dao层写方法就能看出来
项目目录
Students.java
没有变,直接是jdbc的实体类
跳转我的上一篇文章查看实体类代码
C3P0Conn.java
这个基本也是写好就不用变的
package com.web.util;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Conn {
//数据源类
private static ComboPooledDataSource ds = new ComboPooledDataSource();
//获取连接
public static Connection getConnection(){
Connection conn = null;
try {
conn = ds.getConnection();
System.out.println("连接成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//关闭连接
public static void closeAll(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection connection = getConnection();
// closeAll(null,null,connection);
}
}
StuDao.java
这个是主要的业务层,然后在这里写查询方法以及main方法直接运行
每个方法都是一样的套路,多写几个方法就完全记住了
套路
注意总结:
- 只有查询是qr.query(),其他增删改都是qr.update();
- qr打点调方法这行代码,如果这个方法有返回值,就是要复制的。如果没有就不用赋值。
//查询
//1. public 返回值类型 方法名(参数)
//返回值类型是不同的,
//比如通过id查询,因为只有一条数据,所以只需要返回一个学生对象,返回类型就是Students 实体类
//比如查询所有数据,查询到的就是一个集合,所以返回值肯定是List<Students>,尖括号里是泛型,是Students是集体类,每个数据都是一个Students对象
//比如删除不需要返回什么,返回值写void就行
public Students selectById(int id){
//2. 有返回值就新建返回值类型的对象 并赋值为null
Students stu = null;
//3. 固定
conn = C3P0Conn.getConnection();
//4. 写实现方法对应的sql语句
String sql = "select * from stus where id = ?";
//5.qr 连接语句,再根据提示放入try catch块
// 这里把try catch块代码删了,方便观看。想看完整代码下面有
// 连接名,sql语句, 返回值类型, ?占位符
stu = qr.query(conn, sql, new BeanHandler<Students>(Students.class),id);
//6. 写finally块,关闭连接
finally {
C3P0Conn.closeAll(conn);
}
return stu;
}
代码
package com.web.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.web.entity.Students;
import com.web.util.C3P0Conn;
public class StuDao {
private Connection conn = null;
//工具类
private QueryRunner qr = new QueryRunner();
//查询所有
public List<Students> selectStuAll(){
List<Students> list = null;
//获取连接
conn = C3P0Conn.getConnection();
//获取sql语句
String sql = "select * from stus";
//执行sql语句并返回结果
try {
list = qr.query(conn,sql,new BeanListHandler<Students>(Students.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
C3P0Conn.closeAll(conn);
}
return list;
}
//查询
public Students selectById(int id){
Students stu = null;
conn = C3P0Conn.getConnection();
String sql = "select * from stus where id = ?";
try {
// 连接名,sql语句, 返回值类型, ?占位符
stu = qr.query(conn, sql, new BeanHandler<Students>(Students.class),id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
C3P0Conn.closeAll(conn);
}
return stu;
}
//添加
public void addStu(Students stu){
conn = C3P0Conn.getConnection();
String sql = "insert into stus(name,sex,age,birthday,address) values(?,?,?,?,?)";
try {
qr.update(conn, sql,stu.getName(),stu.getSex(),stu.getAge(),stu.getBirthday(),stu.getAddress());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
C3P0Conn.closeAll(conn);
}
}
//修改
public void updateStu(Students stu){
conn = C3P0Conn.getConnection();
String sql = "update stus set name=?,sex=?,age=?,birthday=?,address=? where id = ?";
try {
qr.update(conn, sql,stu.getName(),stu.getSex(),stu.getAge(),stu.getBirthday(),stu.getAddress(),stu.getId());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
C3P0Conn.closeAll(conn);
}
}
//删除
public void delStu(int id ){
conn = C3P0Conn.getConnection();
String sql = "delete from stus where id = ? ";
try {
qr.update(conn, sql,id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
C3P0Conn.closeAll(conn);
}
}
public static void main(String[] args) {
StuDao dao = new StuDao();
//查询一个
//Students stu = dao.selectById(2);
//System.out.println(stu);
//添加
//Students stu = new Students("李四四", "男", 20,"2002-5-3", "三亚市");
//dao.addStu(stu);
//修改
//Students stu = dao.selectById(9);
//可以只改个别
// stu.setName("哈尔滨");
//stu.setAge(22);
// stu.setSex("女");
//dao.updateStu(stu);
//删除
//dao.delStu(9);
//查询所有
List<Students> list = dao.selectStuAll();
System.out.println(list);
}
}