本文主要讲解jdbc连接mySQL。

一、在idea上新建maven工程

二、添加依赖包

<!-- 连接mysql工具包-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version>
</dependency>
<!-- 使用c3p0——jdbc连接池-->
<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.4</version>
</dependency>

三、创建资源目录,并在其中创建c3p0的xml文件

resource可以在工程目录或src下创建

resource/c3p0-config.xml

c3p0-config.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config><!--默认配置--><default-config><!-- initialPoolSize:初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。 --><property name="initialPoolSize">10</property><!-- maxIdleTime:最大空闲时间,30秒内未使用则连接被丢弃。若为0则永不丢弃。--><property name="maxIdleTime">30</property><!-- maxPoolSize:连接池中保留的最大连接数 --><property name="maxPoolSize">100</property><!-- minPoolSize: 连接池中保留的最小连接数 --><property name="minPoolSize">10</property><property name="checkoutTimeout">3000</property></default-config><!--配置连接池mysql--><named-config name="single01mysql"><!--   连接参数  --><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://single01:3306/school?useSSL=false</property><property name="user">root</property><property name="password">ok</property><property name="initialPoolSize">10</property><property name="maxPoolSize">100</property><property name="checkoutTimeout">3000</property><property name="maxIdleTime">30</property><property name="minPoolSize">10</property></named-config><!--配置连接池2,可以配置多个-->
</c3p0-config>

三、建立连接池测试类

public class C3p0Utils {static ComboPooledDataSource source=new ComboPooledDataSource("single01mysql");//创建连接public static Connection getConnection(){Connection connection=null;try {connection=source.getConnection();} catch (SQLException e) {e.printStackTrace();}return connection;}//关闭连接public  static void close(Connection conn, PreparedStatement pstmt, ResultSet rs){try {if (rs!=null) {rs.close();}if (pstmt!=null){pstmt.close();}if (conn!=null){conn.close();}} catch (SQLException e) {e.printStackTrace();}}/* 关闭连接——可变参数public  static void close(AutoCloseable...closes){if (null !=closes){for (AutoCloseable close : closes) {try {close.close();} catch (Exception e) {e.printStackTrace();}}}}*/public static void main(String[] args) {//打印连接for (int i = 0; i < 10; i++) {Connection connection = C3p0Utils.getConnection();System.out.println(connection+ "  "+i);C3p0Utils.close(connection,null,null);
//            C3p0Utils.close(connection);}}
}

四:通过c3p0连接池往MySQL里插入,查询数据

1.创建接口程序,定义插入,查询方法;

public interface IStudentDao {public void insertStudent(List<Student> students);  //插入public List<Student> getStudents();  //查询
}

2、创建要查询的表格的数据的实例类;

属性,构造方法,set/get方法,重写toString方法

public class Student {private String id;private String name;public Student(String id, String name) {this.id = id;this.name = name;}@Overridepublic String toString() {return "Student{" +"id='" + id + '\'' +", name='" + name + '\'' +'}';}public String getId() {return id;}public String getName() {return name;}public void setId(String id) {this.id = id;}public void setName(String name) {this.name = name;}
}

3.创建实现类;实现插入,查询数据

public class StudentDao  implements IStudentDao{//插入mysql表格public void insertStudent(List<Student> students){//insert into student(id,name) values("001","as"),("002","ls");String sql="insert into student(id,name) values";if (students!=null && students.size()>0){for (Student stu : students) {sql=sql+"('"+stu.getId()+"','"+stu.getName()+"'),";}sql=sql.substring(0,sql.length()-1);System.out.println(sql);Connection connection = C3p0Utils.getConnection();PreparedStatement preparedStatement =null;try {preparedStatement =connection.prepareStatement(sql);int i = preparedStatement.executeUpdate();System.out.println(i);} catch (SQLException e) {e.printStackTrace();}finally {C3p0Utils.close(connection,preparedStatement,null);}}}//查询mysql表格public List<Student> getStudents() {Connection connection = C3p0Utils.getConnection();String sql ="select id,name from student";ArrayList<Student> students = new ArrayList<>();PreparedStatement preparedStatement = null;ResultSet resultSet=null;try {preparedStatement=connection.prepareStatement(sql);resultSet = preparedStatement.executeQuery();while (resultSet.next()){String id = resultSet.getString("id");String name = resultSet.getString("name");/*String id = resultSet.getString(1);String name = resultSet.getString(2);*/
//                System.out.println(id+"  "+name);Student student = new Student(id, name);students.add(student);}} catch (SQLException e) {e.printStackTrace();}finally {C3p0Utils.close(connection,preparedStatement,resultSet);}return students;}public static void main(String[] args) {StudentDao studentDao = new StudentDao();//插入mysqlStudent stu1 = new Student("003", "ww");Student stu2 = new Student("004", "zl");Student stu3 = new Student("005", "zq");ArrayList<Student> students = new ArrayList<>();students.add(stu1);students.add(stu2);students.add(stu3);studentDao.insertStudent(students);//查询mysql表格/*List<Student> students = studentDao.getStudents();for (Student stu : students) {System.out.println(stu.toString());}*/}
}

JDBC——c3p0连接池的使用相关推荐

  1. java连接池域名切换_java - 使用JDBC的连接池选项:DBCP与C3P0

    java - 使用JDBC的连接池选项:DBCP与C3P0 什么是可用于Java / JDBC的最佳连接池库? 我正在考虑2个主要候选人(免费/开源): Apache DBCP - [http://c ...

  2. JDBC数据源连接池(1)---DBCP

    何为数据源呢?也就是数据的来源.我在前面的一篇文章<JDBC原生数据库连接>中,采用了mysql数据库,数据来源于mysql,那么mysql就是一种数据源.在实际工作中,除了mysql,往 ...

  3. c3p0和jdbctemplate配置oracle集群rac,C3P0连接池、DRUID连接池和JdbcTemplate

    目录 一.C3P0连接池 1.C3P0连接池简介 2.常用的配置参数 3.C3P0连接池基本使用 (1)C3P0配置文件 (2)API介绍 4.使用步骤 二.DRUID连接池 1. DRUID简介 2 ...

  4. C3P0连接池、DRUID连接池和JdbcTemplate

    目录 一.C3P0连接池 1.C3P0连接池简介 2.常用的配置参数 3.C3P0连接池基本使用 (1)C3P0配置文件 (2)API介绍 4.使用步骤 二.DRUID连接池 1. DRUID简介 2 ...

  5. Spring+Hibernate+c3p0连接池配置-连接无法释放的问题解决方案

     1.Spring+Hibernate+c3p0连接池配置: <?xml version="1.0" encoding="UTF-8"?> < ...

  6. spring配置c3p0连接池、spring的声明式事务管理

    一.spring配置c3p0连接池: 1.导入maven依赖: <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> & ...

  7. (十二)C3P0连接池使用教程

    一般我们在项目中操作数据库时,都是每次需要操作数据库就建立一个连接,操作完成后释放连接.因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒),连接就会被自动释放掉.而每次新建连接都 ...

  8. Hibernate配置C3P0连接池(在配好基本的hibernate配置下使用)

    拷贝jar包 找到我们的hibernate安装包,在lib目录下找到optional目录,打开c3p0文件,拷贝里面的jar包到eclipse里 写一个测试类,代码入下 public class C3 ...

  9. Hibernate C3P0连接池配置

    本文向大家介绍Hibernate C3P0连接池,可能好多人还不了解Hibernate C3P0连接池,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西. Hibernate自带的连接池算 ...

最新文章

  1. C语言优势大揭露,你还在等什么呢?
  2. windows桌面待办事项_记录在电脑便签中的内容怎么在电脑桌面显示透明日历清单?...
  3. linux命令dmesg查看进程被杀死原因
  4. VTK:随机探针用法实战
  5. CImage与OpenCV兼容问题
  6. clear ,refresh,free
  7. Java对象头与monitor
  8. Net设计模式实例之享元模式( Flyweight Pattern)(1)
  9. 黑月MySQL_易语言黑月编译器插件
  10. LATEX参考文献添加文章doi号并嵌入超链接+IEEE期刊缩写查询
  11. 游戏工作室:代理IP老是跳怎么办?试试静态IP吧
  12. 将越狱进行到底 Pod2g邀约众大神组建evad3rs
  13. #研发解决方案#数据开放实验室:再战即席查询和数据开放
  14. PHP 微信支付v3签名生成
  15. ubuntu进行MNN编译
  16. pytcuda学习笔记(一)
  17. 数据库视图view的解析
  18. 为年会写的配乐诗朗诵稿
  19. Java 虚拟机(JVM)原理介绍
  20. 苹果11是高通基带吗_iPhone11信号成最大问题,不支持5G还是英特尔基带,令人失望...

热门文章

  1. FOJ Problem 2121 神庙逃亡
  2. leetcode中等之1949.坚定的友谊
  3. Junit单元测试总结
  4. mybatis-plus 实现自动填充时间
  5. 徐宥——我的大学[转]
  6. Javascript百炼成仙——笔记
  7. css后代选择器:nth_CSS选择器:特异性
  8. [AHK]用AutoHotkey实现中银国际通达信版自动登录
  9. 透视HTTPS建造固若金汤的城堡
  10. stm32f103 TIM1发PWM