JDBC的概述

JDBC的全称是:Java DataBase Connectivity(Java数据库连接)
是一种用于执行sql语句的Java API,可以为多种关系型数据库提供统一的访问。

没有JDBC之前,Java操作数据库需要通过不同的驱动。
有了JDBC之后,Java操作数据库只需通过统一的JDBC标准即可。
image.png

使用JDBC前的准备工作:
第一步:右击项目名称,New/Directory/lib
第二步:在lib文件中存放mysql的连接文件(下载地址如下)

https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11

第三步:关联mysql的连接文件到项目中
在IntelliJ IDEA 中选择File->Project Structure->Modules->Dependencies 点击下面的小加号,选择JARs or directories选择你要添加的外部jar包。

JDBC的API

模板案例

  1. package com.song.jdbc;
  2. import org.junit.Test;
  3. import java.sql.*;
  4. public class Demo1 {
  5. @Test
  6. public void test() {
  7. try {
  8. // 1. 注册驱动
  9. // DriverManager.registerDriver(new Driver());
  10. // 结果:不建议使用
  11. // 理由:因为在加载Driver类的时候就会注册驱动,如果再手动注册一遍,会造成重复注册
  12. Class.forName("com.mysql.cj.jdbc.Driver");
  13. // 结果:建议使用
  14. // 理由:意思是根据类名加载类的实例对象,不会造成重复注册
  15. // 2. 获得连接
  16. Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "???");
  17. // url说明:jdbc(协议) mysql(子协议) localhost(主机名) 3306(端口号)
  18. // url简写:jdbc:mysql///jdbc <= 仅限于本地sql
  19. // 3.1 创建执行sql的对象
  20. Statement statement = connection.createStatement();
  21. // 3.2 执行sql
  22. String sql = "select * from user";
  23. ResultSet resultSet = statement.executeQuery(sql);
  24. while (resultSet.next()) {
  25. int uid = resultSet.getInt("uid");
  26. String username = resultSet.getString("username");
  27. String password = resultSet.getString("password");
  28. String name = resultSet.getString("name");
  29. System.out.println(uid + " " + username + " " + password + " " + name);
  30. }
  31. // 释放资源
  32. resultSet.close();
  33. statement.close();
  34. connection.close();
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }

DriverManager

驱动管理类

作用一:注册驱动
实际开发中注册驱动会使用如下方式,因为之前的方式会导致驱动注册两次

  1. Class.forName("com.mysql.cj.jdbc.Driver");

作用二:获得连接
url说明:jdbc(协议) mysql(子协议) localhost(主机名) 3306(端口号)
url简写:jdbc:mysql///jdbc <= 仅限于本地sql

  1. Connection connection = DriverManager.getConnection(String url, String username, String password)

Connection

连接对象类

作用一:创建执行sql语句的对象
Statement createStatement() : 执行sql语句,有sql注入的漏洞存在
PreparedStatement preparedStatement(String sql) : 预编译sql语句,解决sql注入的漏洞
callableStatement prepareCall(String sql) : 执行sql存储过程

作用二:进行事务的管理
setAutoCommit(boolean autoCommit) : 设置事务是否自动提交
commit() : 事务提交
rollback() : 事务回滚

Statement

sql执行类

作用一:执行sql语句
boolean execute(String sql) : 执行sql,执行select返回true,否则返回false
ResultSet executeQuery(String sql) : 执行sql中的select语句
int executeUpdate(String sql) : 执行sql中的insert/update/delete语句

作用二:执行批处理操作
addBatch(String sql) : 添加到批处理
executeBatch() : 执行批处理
clearBatch() : 清空批处理

ResultSet

结果集类,其实就是查询语句(select)的执行结果的封装

主要作用:获取查询结果
next() : 向前移动一行光标,并判断是否有数据
getXXX() : 针对不同类型获取数据

资源释放

JDBC程序运行完后,切记要释放程序在运行过程中,创建的那些与数据库进行交互的对象,这些对象通常是ResultSet,Statement和Connection对象。

特别是Connection对象,它是非常稀有的资源,用完后必须马上释放,如果Connection不能及时,正确的关闭,极易导致系统宕机。Connection的使用原则是尽量晚创建,早释放。

MySQL5中文参考手册.chm/26.连接器/26.3 之 finally部分

  1. package com.song.jdbc;
  2. import org.junit.Test;
  3. import java.sql.*;
  4. public class Demo1 {
  5. @Test
  6. public void test() {
  7. ResultSet resultSet = null;
  8. Statement statement = null;
  9. Connection connection = null;
  10. try {
  11. // 1. 注册驱动
  12. Class.forName("com.mysql.cj.jdbc.Driver");
  13. // 2. 获得连接
  14. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "???");
  15. // 3.1 创建执行sql的对象
  16. statement = connection.createStatement();
  17. // 3.2 执行sql
  18. String sql = "select * from user";
  19. resultSet = statement.executeQuery(sql);
  20. while (resultSet.next()) {
  21. int uid = resultSet.getInt("uid");
  22. String username = resultSet.getString("username");
  23. String password = resultSet.getString("password");
  24. String name = resultSet.getString("name");
  25. System.out.println(uid + " " + username + " " + password + " " + name);
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. } finally {
  30. // 4. 释放资源
  31. if (resultSet != null) {
  32. try {
  33. resultSet.close();
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. resultSet = null;
  38. }
  39. if (statement != null) {
  40. try {
  41. statement.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. statement = null;
  46. }
  47. if (connection != null) {
  48. try {
  49. connection.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. connection = null;
  54. }
  55. }
  56. }
  57. }

JDBC的增删改查操作

增删改用executeUpdate,查询用executeQuery

新增记录

  1. package com.song.jdbc;
  2. import org.junit.Test;
  3. import java.sql.*;
  4. public class Demo2 {
  5. @Test
  6. public void test() {
  7. Statement statement = null;
  8. Connection connection = null;
  9. try {
  10. // 1. 注册驱动
  11. Class.forName("com.mysql.cj.jdbc.Driver");
  12. // 2. 获得连接
  13. connection = DriverManager.getConnection("jdbc:mysql:///jdbctest", "root", "???");
  14. // 3.1 创建执行sql的对象
  15. statement = connection.createStatement();
  16. // 3.2 执行sql
  17. String sql = "insert into user values (null, 'ccc', '333', '王五')";
  18. int i = statement.executeUpdate(sql);
  19. if (i > 0) {
  20. System.out.println("保存成功!");
  21. }
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. } finally {
  25. // 4. 释放资源
  26. if (statement != null) {
  27. try {
  28. statement.close();
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. statement = null;
  33. }
  34. if (connection != null) {
  35. try {
  36. connection.close();
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. connection = null;
  41. }
  42. }
  43. }
  44. }

修改记录

  1. package com.song.jdbc;
  2. import org.junit.Test;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class Demo3 {
  8. @Test
  9. public void test() {
  10. Statement statement = null;
  11. Connection connection = null;
  12. try {
  13. // 1. 注册驱动
  14. Class.forName("com.mysql.cj.jdbc.Driver");
  15. // 2. 获得连接
  16. connection = DriverManager.getConnection("jdbc:mysql:///jdbctest", "root", "???");
  17. // 3.1 创建执行sql的对象
  18. statement = connection.createStatement();
  19. // 3.2 执行sql
  20. String sql = "update user set username = 'ddd', password='444',name='赵六' where uid = 3";
  21. int i = statement.executeUpdate(sql);
  22. if (i > 0) {
  23. System.out.println("修改成功!");
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. // 4. 释放资源
  29. if (statement != null) {
  30. try {
  31. statement.close();
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. statement = null;
  36. }
  37. if (connection != null) {
  38. try {
  39. connection.close();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. connection = null;
  44. }
  45. }
  46. }
  47. }

删除记录

  1. package com.song.jdbc;
  2. import org.junit.Test;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class Demo4 {
  8. @Test
  9. public void test() {
  10. Statement statement = null;
  11. Connection connection = null;
  12. try {
  13. // 1. 注册驱动
  14. Class.forName("com.mysql.cj.jdbc.Driver");
  15. // 2. 获得连接
  16. connection = DriverManager.getConnection("jdbc:mysql:///jdbctest", "root", "???");
  17. // 3.1 创建执行sql的对象
  18. statement = connection.createStatement();
  19. // 3.2 执行sql
  20. String sql = "delete from user where uid = 3";
  21. int i = statement.executeUpdate(sql);
  22. if (i > 0) {
  23. System.out.println("删除成功!");
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. // 4. 释放资源
  29. if (statement != null) {
  30. try {
  31. statement.close();
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. statement = null;
  36. }
  37. if (connection != null) {
  38. try {
  39. connection.close();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. connection = null;
  44. }
  45. }
  46. }
  47. }

查询记录

  1. package com.song.jdbc;
  2. import org.junit.Test;
  3. import java.sql.*;
  4. public class Demo5 {
  5. @Test
  6. public void test() {
  7. ResultSet resultSet = null;
  8. Statement statement = null;
  9. Connection connection = null;
  10. try {
  11. // 1. 注册驱动
  12. Class.forName("com.mysql.cj.jdbc.Driver");
  13. // 2. 获得连接
  14. connection = DriverManager.getConnection("jdbc:mysql:///jdbctest", "root", "???");
  15. // 3.1 创建执行sql的对象
  16. statement = connection.createStatement();
  17. // 3.2 执行sql
  18. String sql = "select * from user";
  19. resultSet = statement.executeQuery(sql);
  20. while (resultSet.next()) {
  21. int uid = resultSet.getInt("uid");
  22. String username = resultSet.getString("username");
  23. String password = resultSet.getString("password");
  24. String name = resultSet.getString("name");
  25. System.out.println(uid + " " + username + " " + password + " " + name);
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. } finally {
  30. // 4. 释放资源
  31. if (resultSet != null) {
  32. try {
  33. resultSet.close();
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. resultSet = null;
  38. }
  39. if (statement != null) {
  40. try {
  41. statement.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. statement = null;
  46. }
  47. if (connection != null) {
  48. try {
  49. connection.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. connection = null;
  54. }
  55. }
  56. }
  57. }
  1. package com.song.jdbc;
  2. import org.junit.Test;
  3. import java.sql.*;
  4. public class Demo5 {
  5. @Test
  6. public void test() {
  7. ResultSet resultSet = null;
  8. Statement statement = null;
  9. Connection connection = null;
  10. try {
  11. // 1. 注册驱动
  12. Class.forName("com.mysql.cj.jdbc.Driver");
  13. // 2. 获得连接
  14. connection = DriverManager.getConnection("jdbc:mysql:///jdbctest", "root", "???");
  15. // 3.1 创建执行sql的对象
  16. statement = connection.createStatement();
  17. // 3.2 执行sql
  18. String sql = "select * from user where uid = 2";
  19. resultSet = statement.executeQuery(sql);
  20. if (resultSet.next()) {
  21. int uid = resultSet.getInt("uid");
  22. String username = resultSet.getString("username");
  23. String password = resultSet.getString("password");
  24. String name = resultSet.getString("name");
  25. System.out.println(uid + " " + username + " " + password + " " + name);
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. } finally {
  30. // 4. 释放资源
  31. if (resultSet != null) {
  32. try {
  33. resultSet.close();
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. resultSet = null;
  38. }
  39. if (statement != null) {
  40. try {
  41. statement.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. statement = null;
  46. }
  47. if (connection != null) {
  48. try {
  49. connection.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. connection = null;
  54. }
  55. }
  56. }
  57. }

JDBC的工具类

src/jdbc.properties
#右击src文件夹,New/file/jdbc.properties

  1. driverClass = com.mysql.cj.jdbc.Driver
  2. url = jdbc:mysql:///jdbctest
  3. username = root;
  4. password = ???

src/com.song.jdbc.utils

  1. package com.song.jdbc.utils;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.sql.*;
  5. import java.util.Properties;
  6. public class JDBCUtils {
  7. private static final String driverClass;
  8. private static final String url;
  9. private static final String username;
  10. private static final String password;
  11. static {
  12. // 加载属性文件并解析
  13. Properties props = new Properties();
  14. // 如何获得属性文件的输入流?
  15. // 通常情况下使用类的加载器的方式进行获取
  16. InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
  17. try {
  18. props.load(inputStream);
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. driverClass = props.getProperty("driverClass");
  23. url = props.getProperty("url");
  24. username = props.getProperty("username");
  25. password = props.getProperty("password");
  26. }
  27. // 注册驱动的方法
  28. public static void loadDriver() throws ClassNotFoundException {
  29. Class.forName(driverClass);
  30. }
  31. // 获得连接的方法
  32. public static Connection getConnection() throws Exception {
  33. loadDriver();
  34. Connection connection = DriverManager.getConnection(url, username, password);
  35. return connection;
  36. }
  37. // 资源释放
  38. public static void release(Statement statement, Connection connection) {
  39. if (statement != null) {
  40. try {
  41. statement.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. statement = null;
  46. }
  47. if (connection != null) {
  48. try {
  49. connection.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. connection = null;
  54. }
  55. }
  56. public static void release(ResultSet resultSet, Statement statement, Connection connection) {
  57. if (resultSet != null) {
  58. try {
  59. resultSet.close();
  60. } catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. resultSet = null;
  64. }
  65. if (statement != null) {
  66. try {
  67. statement.close();
  68. } catch (SQLException e) {
  69. e.printStackTrace();
  70. }
  71. statement = null;
  72. }
  73. if (connection != null) {
  74. try {
  75. connection.close();
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. connection = null;
  80. }
  81. }
  82. }

src/com.song.jdbc.demo

  1. package com.song.jdbc.demo;
  2. import com.song.jdbc.utils.JDBCUtils;
  3. import org.junit.Test;
  4. import java.sql.*;
  5. public class Demo6 {
  6. @Test
  7. public void test() {
  8. ResultSet resultSet = null;
  9. Statement statement = null;
  10. Connection connection = null;
  11. try {
  12. // 获得连接
  13. connection = JDBCUtils.getConnection();
  14. // 创建执行sql的对象
  15. statement = connection.createStatement();
  16. // 执行sql
  17. String sql = "select * from user where uid = 2";
  18. resultSet = statement.executeQuery(sql);
  19. if (resultSet.next()) {
  20. int uid = resultSet.getInt("uid");
  21. String username = resultSet.getString("username");
  22. String password = resultSet.getString("password");
  23. String name = resultSet.getString("name");
  24. System.out.println(uid + " " + username + " " + password + " " + name);
  25. }
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. } finally {
  29. JDBCUtils.release(resultSet, statement, connection);
  30. }
  31. }
  32. }

JDBC的SQL注入防御

PreparedStatement是Statement的子接口,它的实例对象可以通过调用Connection.preparedStatenebt(sql)方法获得,相对于Statement对象而言:

  • PreparedStatement可以避免SQL注入的问题
  • Statement会使数据库频繁变异SQL,可能造成数据库缓存区溢出。PreparedStatement可对SQL进行预编译,从而提高数据库的执行效率
  • 并且PreparedStatement对于sql中的参数,允许使用占位符的形式进行替换,简化sql语句的编写
  1. public static void login(String username, String password) {
  2. ResultSet resultSet = null;
  3. PreparedStatement preparedStatement = null;
  4. Connection connection = null;
  5. try {
  6. // 获得连接
  7. connection = JDBCUtils.getConnection();
  8. // 编写sql
  9. String sql = "select * from user where username = ? and password = ?";
  10. // 预处理sql
  11. preparedStatement = connection.prepareStatement(sql);
  12. // 设置参数
  13. preparedStatement.setString(1, username);
  14. preparedStatement.setString(1, password);
  15. // 执行sql
  16. resultSet = preparedStatement.executeQuery();
  17. if (resultSet.next()) {
  18. System.out.println("登录成功!");
  19. }else{
  20. System.out.println("登录失败!");
  21. }
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. } finally {
  25. JDBCUtils.release(resultSet, preparedStatement, connection);
  26. }
  27. }

PreparedStatement

新增记录

  1. package com.song.jdbc.demo;
  2. import com.song.jdbc.utils.JDBCUtils;
  3. import org.junit.Test;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. public class Demo8 {
  8. @Test
  9. public void test() {
  10. PreparedStatement preparedStatement = null;
  11. Connection connection = null;
  12. try {
  13. // 获得连接
  14. connection = JDBCUtils.getConnection();
  15. // 编写sql
  16. String sql = "insert into user values (null, ?, ?, ?)";
  17. // 预处理sql
  18. preparedStatement = connection.prepareStatement(sql);
  19. // 设置参数
  20. preparedStatement.setString(1, "ccc");
  21. preparedStatement.setString(2, "333");
  22. preparedStatement.setString(3, "王五");
  23. // 执行sql
  24. int num = preparedStatement.executeUpdate();
  25. if (num >0) {
  26. System.out.println("新增成功!");
  27. }
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. } finally {
  31. JDBCUtils.release(preparedStatement, connection);
  32. }
  33. }
  34. }

修改记录

  1. package com.song.jdbc.demo;
  2. import com.song.jdbc.utils.JDBCUtils;
  3. import org.junit.Test;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. public class Demo8 {
  8. @Test
  9. public void test() {
  10. PreparedStatement preparedStatement = null;
  11. Connection connection = null;
  12. try {
  13. // 获得连接
  14. connection = JDBCUtils.getConnection();
  15. // 编写sql
  16. String sql = "update user set username = ?, password = ?, name = ? where uid =?";
  17. // 预处理sql
  18. preparedStatement = connection.prepareStatement(sql);
  19. // 设置参数
  20. preparedStatement.setString(1, "ddd");
  21. preparedStatement.setString(2, "444");
  22. preparedStatement.setString(3, "赵六");
  23. preparedStatement.setString(4, "4");
  24. // 执行sql
  25. int num = preparedStatement.executeUpdate();
  26. if (num >0) {
  27. System.out.println("修改成功!");
  28. }
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. } finally {
  32. JDBCUtils.release(preparedStatement, connection);
  33. }
  34. }
  35. }

删除记录

  1. package com.song.jdbc.demo;
  2. import com.song.jdbc.utils.JDBCUtils;
  3. import org.junit.Test;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. public class Demo9 {
  7. @Test
  8. public void test() {
  9. PreparedStatement preparedStatement = null;
  10. Connection connection = null;
  11. try {
  12. // 获得连接
  13. connection = JDBCUtils.getConnection();
  14. // 编写sql
  15. String sql = "delete from user where uid = ?";
  16. // 预处理sql
  17. preparedStatement = connection.prepareStatement(sql);
  18. // 设置参数
  19. preparedStatement.setString(1, "4");
  20. // 执行sql
  21. int num = preparedStatement.executeUpdate();
  22. if (num >0) {
  23. System.out.println("删除成功!");
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. JDBCUtils.release(preparedStatement, connection);
  29. }
  30. }
  31. }

查询记录

  1. package com.song.jdbc.demo;
  2. import com.song.jdbc.utils.JDBCUtils;
  3. import org.junit.Test;
  4. import java.sql.*;
  5. public class Demo7 {
  6. @Test
  7. public void test() {
  8. ResultSet resultSet = null;
  9. PreparedStatement preparedStatement = null;
  10. Connection connection = null;
  11. try {
  12. // 获得连接
  13. connection = JDBCUtils.getConnection();
  14. // 编写sql
  15. String sql = "select * from user where uid = ?";
  16. // 预处理sql
  17. preparedStatement = connection.prepareStatement(sql);
  18. // 设置参数
  19. preparedStatement.setString(1, "2");
  20. // 执行sql
  21. resultSet = preparedStatement.executeQuery();
  22. if (resultSet.next()) {
  23. int uid = resultSet.getInt("uid");
  24. String username = resultSet.getString("username");
  25. String password = resultSet.getString("password");
  26. String name = resultSet.getString("name");
  27. System.out.println(uid + " " + username + " " + password + " " + name);
  28. }
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. } finally {
  32. JDBCUtils.release(resultSet, preparedStatement, connection);
  33. }
  34. }
  35. }

连接池C3P0

连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。

image.png

image.png

第一步:在lib文件中存放C3P0的jar包(以下网页的资料里有)
https://class.imooc.com/course/721
第二步:引入C3P0连接池到项目中
在IntelliJ IDEA 中选择File->Project Structure->Modules->Dependencies 点击下面的小加号,选择JARs or directories选择你要添加的外部jar包。

使用1:
#以下代码有个问题,每次执行Demo10的时候,就会创建一个连接池对象,这样是很浪费的。应该抽取出连接方法成static,这样就将只会创建一个连接池对象了,请看使用2。

  1. package com.song.jdbc.demo;
  2. import com.mchange.v2.c3p0.ComboPooledDataSource;
  3. import com.song.jdbc.utils.JDBCUtils;
  4. import org.junit.Test;
  5. import java.sql.*;
  6. public class Demo10 {
  7. @Test
  8. public void test() {
  9. ResultSet resultSet = null;
  10. PreparedStatement preparedStatement = null;
  11. Connection connection = null;
  12. try {
  13. // 创建连接池
  14. ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
  15. // 设置连接池的参数
  16. comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
  17. comboPooledDataSource.setJdbcUrl("jdbc:mysql:///jdbctest");
  18. comboPooledDataSource.setUser("root");
  19. comboPooledDataSource.setPassword("???");
  20. comboPooledDataSource.setMaxPoolSize(20);
  21. comboPooledDataSource.setInitialPoolSize(3);
  22. // 获得连接
  23. connection = comboPooledDataSource.getConnection();
  24. // 编写sql
  25. String sql = "select * from user";
  26. // 预处理sql
  27. preparedStatement = connection.prepareStatement(sql);
  28. // 执行sql
  29. resultSet = preparedStatement.executeQuery();
  30. while (resultSet.next()) {
  31. int uid = resultSet.getInt("uid");
  32. String username = resultSet.getString("username");
  33. String password = resultSet.getString("password");
  34. String name = resultSet.getString("name");
  35. System.out.println(uid + " " + username + " " + password + " " + name);
  36. }
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. } finally {
  40. JDBCUtils.release(resultSet, preparedStatement, connection);
  41. }
  42. }
  43. }

使用2:配置化

  1. package com.song.jdbc.demo;
  2. import com.mchange.v2.c3p0.ComboPooledDataSource;
  3. import com.song.jdbc.utils.JDBCUtils2;
  4. import org.junit.Test;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. public class Demo11 {
  9. @Test
  10. public void test() {
  11. ResultSet resultSet = null;
  12. PreparedStatement preparedStatement = null;
  13. Connection connection = null;
  14. try {
  15. // 获得连接
  16. connection = JDBCUtils2.getConnection();
  17. // 编写sql
  18. String sql = "select * from user";
  19. // 预处理sql
  20. preparedStatement = connection.prepareStatement(sql);
  21. // 执行sql
  22. resultSet = preparedStatement.executeQuery();
  23. while (resultSet.next()) {
  24. int uid = resultSet.getInt("uid");
  25. String username = resultSet.getString("username");
  26. String password = resultSet.getString("password");
  27. String name = resultSet.getString("name");
  28. System.out.println(uid + " " + username + " " + password + " " + name);
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. } finally {
  33. JDBCUtils2.release(resultSet, preparedStatement, connection);
  34. }
  35. }
  36. }

配置文件:src/c3p0-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <c3p0-config>
  3. <default-config>
  4. <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
  5. <property name="jdbcUrl">jdbc:mysql:///jdbctest</property>
  6. <property name="user">root</property>
  7. <property name="password">???</property>
  8. <property name="initialPoolSize">5</property>
  9. <property name="maxPoolSize">20</property>
  10. </default-config>
  11. </c3p0-config>

工具类:src/com.song.jdbc/utils/JDBCUtils2:

  1. package com.song.jdbc.utils;
  2. import com.mchange.v2.c3p0.ComboPooledDataSource;
  3. import java.sql.*;
  4. public class JDBCUtils2 {
  5. private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
  6. // 获得连接的方法
  7. public static Connection getConnection() throws Exception {
  8. Connection connection = dataSource.getConnection();
  9. return connection;
  10. }
  11. // 资源释放
  12. public static void release(Statement statement, Connection connection) {
  13. if (statement != null) {
  14. try {
  15. statement.close();
  16. } catch (SQLException e) {
  17. e.printStackTrace();
  18. }
  19. statement = null;
  20. }
  21. if (connection != null) {
  22. try {
  23. connection.close();
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. connection = null;
  28. }
  29. }
  30. public static void release(ResultSet resultSet, Statement statement, Connection connection) {
  31. if (resultSet != null) {
  32. try {
  33. resultSet.close();
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. resultSet = null;
  38. }
  39. if (statement != null) {
  40. try {
  41. statement.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. statement = null;
  46. }
  47. if (connection != null) {
  48. try {
  49. connection.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. connection = null;
  54. }
  55. }
  56. }