c3p0连接池连接数据库 并增删改查 您所在的位置:网站首页 jdbc操作数据库增删改查 c3p0连接池连接数据库 并增删改查

c3p0连接池连接数据库 并增删改查

2023-09-22 22:21| 来源: 网络整理| 查看: 265

使用c3p0连接池连接数据库

示例(mvc模式)

1.mysql.properties (连接数据库的)文件:

driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true user=root password=root

2.c3p0-config.xml 文件(配置在src根目录下):

com.mysql.jdbc.Driver jdbc:mysql:///test root root 5 10 com.mysql.jdbc.Driver jdbc:mysql:///test root root 5 10

3.util 类:

public class JDBCUtils { // 初始化连接池 private static DataSource dataSource; static { dataSource=new ComboPooledDataSource(); } public static DataSource getDataSource() { return dataSource; } /** * 创建DbUtils常用工具类对象 */ public static QueryRunner getQuerrRunner() { return new QueryRunner(dataSource); } }

注意:区别 在这里插入图片描述 4.dao 层:

/** * 2. 菜系模块,dao接口设计 */ public interface StudentTypeDao { public void save(StudentType studentType);//添加 public void update(StudentType studentType);//更新 public void delete(int id);//删除 public StudentType findById(int id);//根据主键查询 public List getAll();//查询全部 public List getAllForName(String sName);//根据名称查询 }

5.dao 的实现:

/** * 2. 学生系模块dao实现 */ public class StudentTypeDaoimpl implements StudentTypeDao{ @Override public void save(StudentType studentType) {//增加 //SQL语句 String sql="insert into student_list(id,sName,sex,major,nianji) values(?,?,?,?,?)"; try {//抛异常 //把得到的值放入一个数组 Object params[]= {studentType.getId(),studentType.getsName(), studentType.getSex(),studentType.getMajor(),studentType.getNianji()}; //调用util类的getQuerrRunner()方法 JDBCUtils.getQuerrRunner().update(sql,params); } catch (Exception e) { throw new RuntimeException(e); } } @Override public void update(StudentType studentType) {//更新 String sql="update student_list set sName=?,major=?,nianji=? where id=?"; try { Object params[]={studentType.getsName(), studentType.getMajor(),studentType.getNianji(),studentType.getId()}; JDBCUtils.getQuerrRunner().update(sql,params); } catch (Exception e) { throw new RuntimeException(e); } } @Override public void delete(int id) { String sql="delete from student_list where id=?"; try { JDBCUtils.getQuerrRunner().update(sql, id); } catch (Exception e) { throw new RuntimeException(e); } } @Override public StudentType findById(int id) { String sql="select * from student_list where id=?"; try { return JDBCUtils.getQuerrRunner().query(sql, new BeanHandler(StudentType.class), id); } catch (Exception e) { throw new RuntimeException(e); } } @Override public List getAll() { String sql="select * from student_list"; try { List list = JDBCUtils.getQuerrRunner().query(sql, new BeanListHandler(StudentType.class)); return list; } catch (Exception e) { throw new RuntimeException(e); } } @Override public List getAllForName(String sName) { String sql="select * from student_list where sName like ?"; try { return JDBCUtils.getQuerrRunner().query(sql, new BeanListHandler(StudentType.class), "%"+sName+"%"); } catch (Exception e) {; throw new RuntimeException(e); } } }

用到的jar包: 百度网盘: 链接:https://pan.baidu.com/s/1hiied02Gv53yGGxedJEz1A 提取码:u7fi



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有