数据库操作与JDBC

1. JDBC和数据库连接

1.1 JDBC 概述

1.1.1 基本介绍

1. JDBC为访问不同的数据库提供了统一的接口,为使用者屏蔽了细节问题。
2. Java程序员使用JDBC,可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作。

1.1.2 JDBCAPI

1.2 JDBC 快速入门

JDBC 程序编写步骤:
1. 注册驱动-加载Driver类        2. 获取连接-得到Connection
3. 执行增删改查–发送SQL给mysql执行        4. 释放资源-关闭相关连接

JDBC 第一个程序:

import com.mysql.jdbc.Driver;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Properties;
 /**
  * @author  
  * @version 1.0
  * 这是第一个Jdbc 程序,完成简单的操作
*/
 public class Jdbc01 {
   public static void main(String[] args) throws SQLException {
     //前置工作: 在项目下创建一个文件夹比如 libs
    // 将 mysql.jar 拷贝到该目录下,点击 addtoproject..加入到项目中
     //1. 注册驱动
    Driver driver = new Driver();  //创建 driver 对象

    //2. 得到连接
    // 解读
    //(1) jdbc:mysql:// 规定好表示协议,通过jdbc 的方式连接mysql
     //(2) localhost 主机,可以是 ip 地址
    //(3) 3306 表示 mysql 监听的端口
    //(4) hsp_db02 连接到 mysql dbms 的哪个数据库
    //(5) mysql 的连接本质就是前面学过的socket连接
String url = "jdbc:mysql: //localhost:3306/hsp_db02";
     //将 用户名和密码放入到Properties 对象
Properties properties = new Properties();
     //说明 user 和 password 是规定好,后面的值根据实际情况写
properties.setProperty("user", "root");// 用户
properties.setProperty("password", "zak"); //密码
Connection connect = driver.connect(url, properties);

     //3. 执行sql
     //String sql = "insert into actor values(null, '刘德华', '男', '1970-11-11', '110')";
     //String sql = "update actor set name='周星驰' where id = 1";
 String sql = "delete from actor where id = 1";
     //statement 用于执行静态SQL语句并返回其生成的结果的对象
Statement statement = connect.createStatement();
 int rows = statement.executeUpdate(sql); // 如果是 dml 语句,返回的就是影响行数

System.out.println(rows > 0 ? "成功" : "失败");

     //4. 关闭连接资源
 statement.close();
 connect.close();
 }}

1.3 获取数据库连接5种方式

1.3.1 方式1

//获取Driver实现类对象
Driver driver = new com.mysql.jdbc.Driver);
String url = "jdbc:mysql:  //localhost:3306/jdbc_db";
Properties info = new Properties);
info.setProperty("user", "root");
info.setProperty("password", "zak");
Connection conn = driver.connect(url, info);
System.out.println(conn);

1.3.2 方式2

//方式1 会直接使用com.mysql.jdbc.Driver),属于静态加载,灵活性差,依赖强
//---推出--->方式2
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newlnstance();

String url = "jdbc:mysql://localhost:3306/jdbc_db";

Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "abc123");

Connection conn = driver.connect(url, info);
System.out.println(conn);

1.3.3 方式3

//使用DriverManager替换Driver
Class clazz = Class.forName(" com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newlnstance0;

String url = "jdbc:mysql://localhost:3306/jdbc_db";
String user ="root";
String password = "zak";

DriverManager.registerDriver(driver);

Connection conn = DriverManager.getConnection(url, user,password);
System.out.println(conn);

1.3.4 方式4

//使用Class.forName自动完成注册驱动,简化代码=>分析源码
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/jdbc_db";
String user ="root";
String password = "zak";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);

提示:
1. mysqL驱动5.1.6可以无需CLass . forName("com.mysql.jdbc.Driver");
2. 从jdk1.5以后使用了jdbc4,不再需要显示调用class.forName()注册驱动而是自动调用驱动
jar包下META-INF\servicesVava .sql.Driver文本中的类名称去注册
3. 建议还是写上 CLass . forName("com.mysql.jdbc.Driver"),更加明确

1.3.5 方式5

//使用配置文件,连接数据库更灵活
//1.
Connection connection=
DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root","root");
// 中的字符串各个值,比如端口,数据库,用户名,密码为了方便,我们可以将信息写入到.properties文件中,方便操作

// 2. 
jdbc.properties
    user = root
    password = root
    url = jdbc:mysql: //localhost:3306/girls
    driver = com.mysql.jdbc.Driver
import com.mysql.jdbc.Driver;
 import org.junit.jupiter.api.Test;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.util.Properties;
/**
    * @author 
    * @version 1.0
     * 分析java 连接mysql的5中方式
*/
 public class JdbcConn {
     //方式1
     @Test
     public void connect01() throws SQLException {
         Driver driver = new Driver(); //创建 driver 对象
        String url = "jdbc:mysql://localhost:3306/zak_db02";
         //将 用户名和密码放入到Properties 对象
        Properties properties = new Properties();
         //说明 user 和 password 是规定好,后面的值根据实际情况写
        properties.setProperty("user", "root");// 用户
        properties.setProperty("password", "zak"); //密码
        Connection connect = driver.connect(url, properties);
         System.out.println(connect);
     }

     //方式2
     @Test
     public void connect02() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
     //使用反射加载Driver类 , 动态加载,更加的灵活,减少依赖性
     Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
     Driver driver = (Driver)aClass.newInstance();

     String url = "jdbc:mysql://localhost:3306/zak_db02";
     //将 用户名和密码放入到Properties 对象
    Properties properties = new Properties();
     //说明 user 和 password 是规定好,后面的值根据实际情况写
    properties.setProperty("user", "root");// 用户
    properties.setProperty("password", "zak"); //密码

    Connection connect = driver.connect(url, properties);
     System.out.println("方式 2=" + connect);
     }

    //方式3 使用DriverManager 替代 driver 进行统一管理
    @Test
    public void connect03() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
        //使用反射加载Driver
         Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
         Driver driver = (Driver) aClass.newInstance();

        //创建url 和 user 和 password
         String url = "jdbc:mysql://localhost:3306/zak_db02";
         String user = "root";
         String password = "zak";
 
        DriverManager.registerDriver(driver); //注册 Driver 驱动
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("第三种方式=" + connection);
 }

    //方式4: 使用Class.forName 自动完成注册驱动,简化代码
    //这种方式获取连接是使用的最多,推荐使用
    @Test
     public void connect04() throws ClassNotFoundException, SQLException {
     //使用反射加载了 Driver类
    //在加载 Driver 类时,完成注册
    /*
    源码:1. 静态代码块,在类加载时,会执行一次.
     2. DriverManager.registerDriver(new Driver());
     3. 因此注册driver的工作已经完成
    static {
         try {
             DriverManager.registerDriver(new Driver());
         } catch (SQLException var1) {
             throw new RuntimeException("Can't register driver!");
     } }
     */

        Class.forName("com.mysql.jdbc.Driver");
         //创建url 和 user 和 password
         String url = "jdbc:mysql://localhost:3306/zak_db02";
         String user = "root";
         String password = "zak";
         Connection connection = DriverManager.getConnection(url, user, password);
         System.out.println("第 4 种方式~ " + connection);
     }

     //方式5, 在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
    @Test
     public void connect05() throws IOException, ClassNotFoundException, SQLException {

     //通过Properties 对象获取配置文件的信息
        Properties properties = new Properties();
     //获取相关的值
    properties.load(new FileInputStream("src\\mysql.properties"));
     String user = properties.getProperty("user");
     String password = properties.getProperty("password");
     String driver = properties.getProperty("driver");
     String url = properties.getProperty("url");

     Class.forName(driver);//建议写上
    Connection connection = DriverManager.getConnection(url, user, password);

     System.out.println("方式 5 " + connection);
  } }

1.4 ResultSet[结果集]

1.4.1 基本介绍

1. 表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
2. ResultSet对象保持一个光标指向其当前的数据行。最初,光标位于第一行之前
3. next方法将光标移动到下一行,并且由于在ResultSet对象中没有更多行时返回false,因此可以在while循环中使用循环来遍历结果集

1.4.2 应用实例

 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.sql.*;
 import java.util.Properties;
 /**
     * @author 
     * @version 1.0
     * 演示select 语句返回 ResultSet,并取出结果
*/
@SuppressWarnings({"all"})
 public class ResultSet_ {
     public static void main(String[] args) throws Exception {
     //通过Properties 对象获取配置文件的信息
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\mysql.properties"));
     //获取相关的值
     String user = properties.getProperty("user");
     String password = properties.getProperty("password");
     String driver = properties.getProperty("driver");
     String url = properties.getProperty("url");

    //1. 注册驱动
    Class.forName(driver);//建议写上
    //2. 得到连接
    Connection connection = DriverManager.getConnection(url, user, password);
    //3. 得到Statement
     Statement statement = connection.createStatement();
     //4. 组织SqL
     String sql = "select id, name , sex, borndate from actor";
     //执行给定的SQL语句,该语句返回单个 ResultSet对象

/*
 +----+-----------+-----+---------------------+
 | id | name | sex | borndate     |
 +----+-----------+-----+---------------------+-------+
 | 4 | 刘德华 | 男 |1970-12-1200:00:00|
 |  5 | jack  | 男 |1990-11-1100:00:00 |
 +----+-----------+-----+---------------------+-------+
 */
//阅读debug 代码 resultSet 对象的结构
    ResultSet resultSet = statement.executeQuery(sql);
 //5. 使用while 取出数据
    while (resultSet.next()) { // 让光标向后移动,如果没有更多行,则返回false
    int id =resultSet.getInt(1);//获取该行的第1列
    //int id1 =resultSet.getInt("id");通过列名来获取值,推荐
    String name=resultSet.getString(2);//获取该行的第2列
    String sex =resultSet.getString(3);
     Date date =resultSet.getDate(4);
    System.out.println(id+"\t"+name+"\t"+sex+"\t"+date);
 }
 //6.关闭连接
    resultSet.close();
     statement.close();
     connection.close();
 } }

1.5 Statement

1.5.1 基本介绍

1. Statement对象用于执行静态SQL语句并返回其生成的结果的对象
2. 在连接建立后,需要对数据库进行访问,执行命名或是SQL语句,可以通过
        Statement [存在SQL注入]
        PreparedStatement [预处理]
        CallableStatement [存储过程]
3. Statement对象执行SQL语句,存在SQL注入风险
4. SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的SQL语句段或命令,恶意攻击数据库。
5. 要防范SQL注入,只要用 PreparedStatement(从Statement扩展而来)取代Statement就可以了.

--演示sql注入
--创建一张表
CREATE TABLE admin(--管理员表
NAME VARCHAR(32) NOT NULL UNIQUE,
 pwd VARCHAR(32) NOT NULL DEFAULT ") CHARACTER SET utf8;

--添加数据
INSERT INTO admin VALUES('tom', '123');

--查找某个管理是否存在

SELECT *
 FROM admin
 WHERE NAME ='tom'AND pwd ='123'
 
--SQL
--输入用户名为1'or
--输入万能密码为 or'1'='1
SELECT *
     FROM admin
     WHERE NAME ='1'OR'AND pwd ='OR'1'='1'
SELECT * FROM admin

1.5.2 案例:

 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.sql.*;
 import java.util.Properties;
 import java.util.Scanner;
 /**
   * @author 
   * @version 1.0
   * 演示statement 的注入问题
*/
@SuppressWarnings({"all"})
 public class Statement_ {
     public static void main(String[] args) throws Exception {
         Scanner scanner = new Scanner(System.in);
         //让用户输入管理员名和密码
        System.out.print("请输入管理员的名字: "); //next(): 当接收到 空格或者 '就是表示结束
        String admin_name = scanner.nextLine(); // 说明,如果希望看到 SQL 注入,这里需要用nextLine
        System.out.print("请输入管理员的密码: ");
         String admin_pwd = scanner.nextLine();
        //通过Properties 对象获取配置文件的信息

        Properties properties = new Properties();
         properties.load(new FileInputStream("src\\mysql.properties"));
         //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
         String driver = properties.getProperty("driver");
         String url = properties.getProperty("url");

     //1. 注册驱动
        Class.forName(driver);//建议写上

    //2. 得到连接
        Connection connection = DriverManager.getConnection(url, user, password);

     //3. 得到Statement
         Statement statement = connection.createStatement();
    //4. 组织SqL
         String sql = "select name , pwd from admin where name ='"+ admin_name + "' and pwd = '" + admin_pwd + "'";
         ResultSet resultSet = statement.executeQuery(sql);
         if (resultSet.next()) { //如果查询到一条记录,则说明该管理存在
             System.out.println("恭喜, 登录成功")
     } else {
            System.out.println("对不起,登录失败");
     }
     //关闭连接
        resultSet.close();
         statement.close();
         connection.close();
    }}

1.6 PreparedStatement

1.6.1 基本介绍

编写sql:
String sql ="SELECT COUNT(*) FROM admin WHERE username =? AND PASSWORD=?";
1. PreparedStatement 执行的SQL语句中的参数用问号(?)来表示,调用PreparedStatement对象的setXxx()方法来设置这些参数. setXxx()方法有两个参数,第一个参数是要设置的SQL语句中的参数的索引(从1开始),第二个是设置的SQL语句中的参数的值
2. 调用executeQuery0),返回ResultSet 对象
3. 调用executeUpdate(): 执行更新,包括增、删、修改

1.6.2 预处理好处

1. 不再使用+拼接sql语句,减少语法错误
2. 有效的解决了sql注入问题!
3. 大大减少了编译次数,效率较高
案例:

 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.sql.*;
 import java.util.Properties;
 import java.util.Scanner;
 /**
   * @author 
   * @version 1.0
   * 演示PreparedStatement 使用
*/
 @SuppressWarnings({"all"})
 public class PreparedStatement_ {
     public static void main(String[] args) throws Exception {
         //看 PreparedStatement 类图

            Scanner scanner = new Scanner(System.in);

         //让用户输入管理员名和密码
        System.out.print("请输入管理员的名字: "); //next(): 当接收到 空格或者 '就是表示结束
        String admin_name = scanner.nextLine(); // 说明,如果希望看到 SQL 注入,这里需要用nextLine
         System.out.print("请输入管理员的密码: ");
         String admin_pwd = scanner.nextLine();

         //通过Properties 对象获取配置文件的信息
         Properties properties = new Properties();
         properties.load(new FileInputStream("src\\mysql.properties"));
         //获取相关的值
        String user = properties.getProperty("user");
         String password = properties.getProperty("password");
         String driver = properties.getProperty("driver");
         String url = properties.getProperty("url");

 //1. 注册驱动
Class.forName(driver);//建议写上
//2. 得到连接
Connection connection = DriverManager.getConnection(url, user, password)
 //3. 得到PreparedStatement
 //3.1 组织 SqL, Sql 语句的 ? 就相当于占位符
String sql = "select name , pwd from admin where name =? and pwd = ?";
 //3.2 preparedStatement 对象实现了 PreparedStatement 接口的实现类的对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//3.3 给 ? 赋值
 preparedStatement.setString(1, admin_name);
 preparedStatement.setString(2, admin_pwd);

 //4. 执行 select 语句使用 executeQuery
//如果执行的是 dml(update,insert ,delete) executeUpdate()
//这里执行 executeQuery,不要在写 sql

ResultSetresultSet=preparedStatement.executeQuery(sql);
 if(resultSet.next()){//如果查询到一条记录,则说明该管理存在
     System.out.println("恭喜,登录成功");
 }else{
     System.out.println("对不起,登录失败");
 }

//关闭连接
resultSet.close();
 preparedStatement.close();
 connection.close();
 } }
 import java.io.FileInputStream;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.Properties;
 import java.util.Scanner;
/**
   * @author 
   * @version 1.0
   * 演示PreparedStatement 使用 dml 语句
*/
 @SuppressWarnings({"all"})
 public class PreparedStatementDML_ {
     public static void main(String[] args) throws Exception {
     //看 PreparedStatement 类图

        Scanner scanner = new Scanner(System.in);

     //让用户输入管理员名和密码
    System.out.print("请输删除管理员的名字: "); //next(): 当接收到 空格或者 '就是表示结束
    String admin_name = scanner.nextLine(); // 说明,如果希望看到 SQL 注入,这里需要用nextLine
 //System.out.print("请输入管理员的新密码: ");
 //String admin_pwd = scanner.nextLine();
 
//通过Properties 对象获取配置文件的信息

    Properties properties = new Properties();
     properties.load(new FileInputStream("src\\mysql.properties"));
 //获取相关的值
    String user = properties.getProperty("user");
     String password = properties.getProperty("password");
    String driver = properties.getProperty("driver");
     String url = properties.getProperty("url");

 //1. 注册驱动
Class.forName(driver); //建议写上

//2. 得到连接
Connection connection = DriverManager.getConnection(url, user, password);

 //3. 得到PreparedStatement
 //3.1 组织 SqL, Sql 语句的 ? 就相当于占位符
//添加记录
//String sql = "insert into admin values(?, ?)";
 //String sql = "update admin set pwd = ? where name = ?";
     String sql = "delete from admin where name = ?";
 //3.2 preparedStatement 对象实现了 PreparedStatement 接口的实现类的对象
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
 //3.3 给 ? 赋值
    preparedStatement.setString(1, admin_name);
 //preparedStatement.setString(2, admin_name);

 //4. 执行 dml 语句使用 executeUpdate
     int rows = preparedStatement.executeUpdate();
     System.out.println(rows > 0 ? "执行成功" : "执行失败");
 //关闭连接
    preparedStatement.close();
     connection.close();
 } }

1.7 JDBC 的相关API 小结

1.8 封装JDBCUtils【关闭连接,得到连接】

1.8.1 说明

在jdbc操作中,获取连接和释放资源是经常使用到,可以将其封装JDBC连接的工真类JDBCUtils

1.8.2 代码实现

实际使用使用工具类JDBCUtils:

 import java.io.FileInputStream;
 import java.io.IOException;
 import java.sql.*;
 import java.util.Properties;

 /**
   *@author
   *@version1.0
   *这是一个工具类,完成mysql的连接和关闭资源
*/
 public class JDBCUtils {
     //定义相关的属性(4个), 因为只需要一份,因此,我们做出static
     private static String user; //用户名
    private static String password; //密码
    private static String url; //url
     private static String driver; //驱动名

    //在 static 代码块去初始化
    static {

        try {
             Properties properties = new Properties();
             properties.load(new FileInputStream("src\\mysql.properties"));
             //读取相关的属性值
             user = properties.getProperty("user");
             password = properties.getProperty("password");
             url = properties.getProperty("url");
             driver = properties.getProperty("driver");
         } catch (IOException e) {
            //在实际开发中,我们可以这样处理
            //1. 将编译异常转成 运行异常
            //2. 调用者,可以选择捕获该异常,也可以选择默认处理该异常,比较方便.
             throw new RuntimeException(e);
        }}

 //连接数据库, 返回Connection
 public static Connection getConnection() {
     try {
         return DriverManager.getConnection(url, user, password);
     } catch (SQLException e) {
         //1. 将编译异常转成 运行异常
        //2. 调用者,可以选择捕获该异常,也可以选择默认处理该异常,比较方便.
     throw new RuntimeException(e);
     } }

 //关闭相关资源
/*
     1. ResultSet 结果集
     2. Statement 或者 PreparedStatement
     3. Connection
     4. 如果需要关闭资源,就传入对象,否则传入 null
 */

 public static void close(ResultSet set, Statement statement, Connection connection) {
     //判断是否为null
     try {
         if (set != null) {
        set.close();
     }
      if(statement != null){
         statement.close();
     }
     if(connection != null){
         connection.close();
     }
     }catch(SQLExceptione){
         //将编译异常转成运行异常抛出
        thrownewRuntimeException(e);
 } } }
 import org.junit.jupiter.api.Test;
 import java.sql.*;

/**
    *@version1.0
    * 该类演示如何使用JDBCUtils工具类,完成dml 和 select
 */
 public class JDBCUtils_Use {
     @Test
     public void testSelect() {
         //1. 得到连接
        Connection connection = null;
      //2. 组织一个sql
     String sql = "select * from actor where id = ?";
     PreparedStatement preparedStatement = null;
     ResultSet set = null;
     //3. 创建PreparedStatement 对象
     try {
         connection = JDBCUtils.getConnection();
        System.out.println(connection.getClass()); //com.mysql.jdbc.JDBC4Connection
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 5); //给?号赋值
         //执行, 得到结果集
        set = preparedStatement.executeQuery();
     //遍历该结果集
    while (set.next()) {
         int id = set.getInt("id");
         String name = set.getString("name");
         String sex = set.getString("sex");
        Date borndate = set.getDate("borndate");
         String phone = set.getString("phone");
         System.out.println(id + "\t" + name + "\t" + sex + "\t" + borndate + "\t" + phone);
     }
     } catch (SQLException e) {
         e.printStackTrace();
     } finally {
         //关闭资源
        JDBCUtils.close(set, preparedStatement, connection);
     } }

    @Test
    public void testDML() {//insert , update, delete
         //1. 得到连接
        Connection connection = null;
         //2. 组织一个sql
         String sql = "update actor set name = ? where id = ?";
         // 测试 delete 和 insert ,自己玩.
         PreparedStatement preparedStatement = null;
        //3. 创建PreparedStatement 对象
      try {
         connection = JDBCUtils.getConnection();
         preparedStatement = connection.prepareStatement(sql);
        //给占位符赋值
        preparedStatement.setString(1, "周星驰");
         preparedStatement.setInt(2, 4);
         //执行
        preparedStatement.executeUpdate();
     } catch (SQLException e) {
         e.printStackTrace();
     } finally {
         //关闭资源
    JDBCUtils.close(null, preparedStatement, connection);
 } } }

1.9 事务

1.9.1 基本介绍

1. JDBC程序中当一个Connection对象创建时,默认情况下是自动提交事务:每次执行一个SQL语句时,如果执行成功,就会向数据库自动提交,而不能回滚。
2. JDBC程序中为了让多个SQL语句作为一个整体执行,需要使用事务
3. 调用Connection的setAutoCommit(false)可以取消自动提交事务
4. 在所有的SQL语句都成功执行后,调用Connection的commit( ) ;方法提交事务
5. 在其中某个操作失败或出现异常时,调用 Connection的rollback(); 方法回滚事务

1.9.2 应用实例

模拟经典的转账业务

1.9.3 使用事务解决问题-模拟经典的转账业务

 import com.zakedu.jdbc.utils.JDBCUtils;
 import org.junit.jupiter.api.Test;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 /**
  *@author
  *@version1.0
  *演示jdbc中如何使用事务
*/
 public class Transaction_{
    //没有使用事务.
     @Test
     public void noTransaction() {
         //操作转账的业务
        //1. 得到连接
        Connection connection = null;
         //2. 组织一个sql
         String sql = "update account set balance = balance- 100 where id = 1";
       String sql2 = "update account set balance = balance + 100 where id = 2";
         PreparedStatement preparedStatement = null;
         //3. 创建PreparedStatement 对象
        try {
        connection = JDBCUtils.getConnection(); // 在默认情况下,connection 是默认自动提交
         preparedStatement = connection.prepareStatement(sql);
         preparedStatement.executeUpdate(); // 执行第 1 条 sql
         int i = 1 / 0; //抛出异常
        preparedStatement = connection.prepareStatement(sql2);
         preparedStatement.executeUpdate(); // 执行第 3 条 sql
     } catch (SQLException e) {
         e.printStackTrace();
     } finally {
     //关闭资源
        JDBCUtils.close(null, preparedStatement, connection);
     } }

 //事务来解决
    @Test
     public void useTransaction() {
 //操作转账的业务
//1. 得到连接
    Connection connection = null;
 //2. 组织一个sql
     String sql = "update account set balance = balance- 100 where id = 1";
     String sql2 = "update account set balance = balance + 100 where id = 2";
     PreparedStatement preparedStatement = null;
 //3. 创建PreparedStatement 对象
    try {
     connection = JDBCUtils.getConnection(); // 在默认情况下,connection 是默认自动提交
//将 connection 设置为不自动提交
    connection.setAutoCommit(false); //开启了事务
    preparedStatement = connection.prepareStatement(sql);
     preparedStatement.executeUpdate(); // 执行第 1 条 sql
     int i = 1 / 0; //抛出异常
    preparedStatement = connection.prepareStatement(sql2);
     preparedStatement.executeUpdate(); // 执行第 3 条 sql
//这里提交事务
    connection.commit();
    
    }catch(SQLExceptione){
 //这里我们可以进行回滚,即撤销执行的SQL
 //默认回滚到事务开始的状态.
     System.out.println("执行发生了异常,撤销执行的sql");
     try{
         connection.rollback();
     }catch(SQLExceptionthrowables){
         throwables.printStackTrace();
     }
         e.printStackTrace();
     }finally{
 //关闭资源
        JDBCUtils.close(null,preparedStatement,connection);
 } } }

1.10 批处理

1.10.1 基本介绍

1. 当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率。
2. JDBC的批量处理语句包括下面方法:
        addBatch(): 添加需要批量处理的SQL语句或参数
        executeBatch(): 执行批量处理语句;
        clearBatch():清空批处理包的语句
3. JDBC连接MySQL时,如果要使用批处理功能,请再url中加参数?
        rewriteBatchedStatements = true
4. 批处理往往和PreparedStatement一起搭配使用,可以既减少编译次数,又减少运行次数,效率大大提高

1.10.2 应用实例

1. 演示向admin2表中添加5000条数据,看着使用批处理耗时多久
2. 注意:需要修改配置文件 jdbc.propertiesurl = jdbc:mysql://localhost:3306/数据库?
        rewriteBatchedStatements=true
create table admin2
(id int primary key auto_increment ,username varchar(32) not null,password varchar(32) not null);

 import com.zakedu.jdbc.utils.JDBCUtils;
 import org.junit.jupiter.api.Test;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 /**
  * @author 
  * @version 1.0
  * 演示java的批处理
*/
 public class Batch_ {
     //传统方法,添加5000条数据到admin2
     @Test
     public void noBatch() throws Exception 
     Connection connection = JDBCUtils.getConnection();
     String sql = "insert into admin2 values(null, ?, ?)";
     PreparedStatement preparedStatement = connection.prepareStatement(sql);
     System.out.println("开始执行");
     long start = System.currentTimeMillis();//开始时间
    for (int i = 0; i < 5000; i++) {//5000 执行
        preparedStatement.setString(1, "jack" + i);
         preparedStatement.setString(2, "666");
         preparedStatement.executeUpdate();
     }
    long end = System.currentTimeMillis();
    System.out.println("传统的方式 耗时=" + (end- start));//传统的方式 耗时=10702
    //关闭连接
    JDBCUtils.close(null, preparedStatement, connection);
 }

 //使用批量方式添加数据
 @Test
  public void batch() throws Exception {
     Connection connection = JDBCUtils.getConnection();
     String sql = "insert into admin2 values(null, ?, ?)";
     PreparedStatement preparedStatement = connection.prepareStatement(sql);
     System.out.println("开始执行");
     long start = System.currentTimeMillis();//开始时间
    for (int i = 0; i < 5000; i++) {//5000 执行
        preparedStatement.setString(1, "jack" + i);
         preparedStatement.setString(2, "666");
         //将 sql 语句加入到批处理包中-> 看源码
        /*
         //1. //第一就创建 ArrayList- elementData => Object[]
         //2. elementData => Object[] 就会存放我们预处理的sql 语句
        //3. 当 elementData 满后,就按照 1.5 扩容
        //4. 当添加到指定的值后,就executeBatch
         //5. 批量处理会减少我们发送sql语句的网络开销,而且减少编译次数,因此效率提高
    public void addBatch() throws SQLException {
     synchronized(this.checkClosed().getConnectionMutex()) {
      if (this.batchedArgs == null) {
        this.batchedArgs = new ArrayList();
     }
     for(int i = 0; i < this.parameterValues.length; ++i) {
       this.checkAllParametersSet(this.parameterValues[i], this.parameterStreams[i], i);
     }
     this.batchedArgs.add(new  PreparedStatement.BatchParams(this.parameterValues, this.parameterStreams, this.isStream, this.streamLengths, this.isNull));
     } }
     */
     preparedStatement.addBatch();
     //当有1000 条记录时,在批量执行
    if((i + 1) % 1000 == 0) {//满 1000 条 sql
     preparedStatement.executeBatch();
     //清空一把
     preparedStatement.clearBatch()
    }}
    long end = System.currentTimeMillis();
     PreparedStatement.BatchParams(this.parameterValues,
     System.out.println("批量方式 耗时=" + (end- start));//批量方式 耗时=108
     //关闭连接
    JDBCUtils.close(null, preparedStatement, connection);
}}

1.11 数据库连接池

1.11.1 5k次连接数据库问题

        1.编写程序完成连接MySQL5000次的操作
        2.看看有什么问题,耗时又是多久.=>数据库连接池

 import com.zakedu.jdbc.utils.JDBCUtils;
 import org.junit.jupiter.api.Test;
 import java.sql.Connection;
 public class ConQuestion{
     //代码连接mysql5000次
    @Test
    public void testCon(){
      //看看连接-关闭connection会耗用多久
      long start = System.currentTimeMillis();
      System.out.println("开始连接.....");
      for(inti=0;i<5000;i++){
      //使用传统的jdbc方式,得到连接
     Connection connection = JDBCUtils.getConnection();
     //做一些工作,比如得到PreparedStatement,发送sql
    //关闭
     JDBCUtils.close(null,null,connection);
     }
    long end = System.currentTimeMillis();
     System.out.println("传统方式5000次耗时="+(end-start)); //传统方式5000次耗时=7099
 } }

1.11.2 传统获取Connection问题分析

1. 传统的JDBC数据库连接使用 DriverManager 来获取,每次向数据库建立连接的时候都要将Connection 加载到内存中,再验证IP地址,用户名和密码(0.05s ~1s时间)。需要数据库连接的时候,就向数据库要求一个,频繁的进行数据库连接操作将占用很多的系统资源,容易造成服务器崩溃。
2. 每一次数据库连接,使用完后都得断开,如果程序出现异常而未能关闭,将导致数据库内存泄漏,最终将导致重启数据库。
3. 传统获取连接的方式,不能控制创建的连接数量,如连接过多,也可能导致内存泄漏,MySQL崩溃。
4. 解决传统开发中的数据库连接问题,可以采用数据库连接池技术(connection pool)。

1.11.3 数据库连接池种类

1. JDBC的数据库连接池使用javax.sql.DataSource来表示,DataSource只是一个接口,该接口通常由第三方提供实现[提供.jar]
2. C3PO数据库连接池,速度相对较慢,稳定性不错(hibernate, spring)
3. DBCP数据库连接池,速度相对c3p0较快,但不稳定
4. Proxool数据库连接池,有监控连接池状态的功能,稳定性较c3p0差一点
5. BoneCP数据库连接池,速度快
6. Druid(德鲁伊)是阿里提供的数据库连接池,集DBCP、C3PO、Proxool优点于一身的数据库连接池

1.11.4 C3P0 应用实例

使用代码实现c3p0数据库连接池,配置文件放src自录下 

import com.mchange.v2.c3p0.ComboPooledDataSource;
 import org.junit.jupiter.api.Test;
 import java.io.FileInputStream;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Properties;
 /**
  * 演示c3p0的使用
*/
public class C3P0_ {
     //方式1: 相关参数,在程序中指定user,url,password等
     @Test
     public void testC3P0_01() throws Exception {
         //1. 创建一个数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
     //2. 通过配置文件mysql.properties 获取相关连接的信息
        Properties properties = new Properties();
     properties.load(new FileInputStream("src\\mysql.properties"));
     //读取相关的属性值
    String user = properties.getProperty("user");
    String password = properties.getProperty("password");
     String url = properties.getProperty("url");
     String driver = properties.getProperty("driver");

    //给数据源 comboPooledDataSource 设置相关的参数
    //注意:连接管理是由 comboPooledDataSource 来管理
    comboPooledDataSource.setDriverClass(driver);
     comboPooledDataSource.setJdbcUrl(url);
     comboPooledDataSource.setUser(user);
     comboPooledDataSource.setPassword(password);

     //设置初始化连接数
    comboPooledDataSource.setInitialPoolSize(10);
     //最大连接数
    comboPooledDataSource.setMaxPoolSize(50);
     //测试连接池的效率, 测试对mysql5000次操作
    long start = System.currentTimeMillis();
     for (int i = 0; i < 5000; i++) {
        Connection connection = comboPooledDataSource.getConnection(); //这个方法就是从 DataSource 接口实现的
    //System.out.println("连接 OK");
         connection.close();
    }
     long end = System.currentTimeMillis();
     //c3p0 5000 连接 mysql 耗时=391
     System.out.println("c3p0 5000 连接 mysql 耗时=" + (end- start));
    }
    //第二种方式 使用配置文件模板来完成
    //1. 将 c3p0 提供的 c3p0.config.xml 拷贝到 src目录下
    //2. 该文件指定了连接数据库和连接池的相关参数
    @Test
     public void testC3P0_02() throws SQLException {
         ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("zak_edu");
     //测试5000 次连接mysql
     long start = System.currentTimeMillis();
     System.out.println("开始执行....");
     for (int i = 0; i < 500000; i++) {
         Connection connection = comboPooledDataSource.getConnection();
         //System.out.println("连接 OK~");
        connection.close();
    }
     long end = System.currentTimeMillis();
     //c3p0 的第二种方式 耗时=413
     System.out.println("c3p0 的第二种方式(500000) 耗时=" + (end- start)); //1917
   } }

1.11.5 Druid(德鲁伊)应用实例

        使用代码实现Druid(德鲁伊)数据库连接池

 import com.alibaba.druid.pool.DruidDataSourceFactory;
 import org.junit.jupiter.api.Test;
 import javax.sql.DataSource;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.sql.Connection;
 import java.util.Properties;

 public class Druid_{
     @Test
    public void testDruid() throws Exception {
     //1. 加入 Druid jar 包
    //2. 加入 配置文件 druid.properties , 将该文件拷贝项目的src目录
    //3. 创建Properties 对象, 读取配置文件
    Properties properties = new Properties();
     properties.load(new FileInputStream("src\\druid.properties"));

 //4. 创建一个指定参数的数据库连接池,Druid连接池
    DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

     long start = System.currentTimeMillis();
     for (int i = 0; i < 500000; i++) {
         Connection connection = dataSource.getConnection();
         System.out.println(connection.getClass());
         //System.out.println("连接成功!");
         connection.close();
     }
     long end = System.currentTimeMillis();
     //druid 连接池 操作5000 耗时=412
     System.out.println("druid 连接池 操作 500000 耗时=" + (end- start));//539
 } }

1.11.6 将JDBCUtils工具类改成Druid(德鲁伊)实现

        通过德鲁伊数据库连接池获取连接对象

 import com.alibaba.druid.pool.DruidDataSourceFactory;
 import javax.sql.DataSource;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Properties;

 public class JDBCUtilsByDruid{
     private static DataSource ds;
     //在静态代码块完成 ds初始化
    static {
     Properties properties = new Properties();
     try {
         properties.load(new FileInputStream("src\\druid.properties"));
         ds = DruidDataSourceFactory.createDataSource(properties);
     } catch (Exception e) {
     e.printStackTrace();
 } }

 //编写getConnection 方法
public static Connection getConnection() throws SQLException {
     return ds.getConnection();
 }

 //关闭连接, 再次强调: 在数据库连接池技术中,close 不是真的断掉连接
//而是把使用的Connection对象放回连接池
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
 try {
     if (resultSet != null) {
        resultSet.close();
 }
  if(statement!=null){
     statement.close();
 }
 if(connection!=null){
     connection.close();
 }
 }catch(SQLExceptione){
     throw new RuntimeException(e);
 } } }
import org.junit.jupiter.api.Test;
 import java.sql.*;
 import java.util.ArrayList;

@SuppressWarnings({"all"})
 public class JDBCUtilsByDruid_USE {
     @Test
     public void testSelect() {
     System.out.println("使用 druid 方式完成");
 //1. 得到连接
Connection connection = null;
 //2. 组织一个sql
 String sql = "select * from actor where id >= ?";
 PreparedStatement preparedStatement = null;
 ResultSet set = null;
 //3. 创建PreparedStatement 对象
try {
     connection = JDBCUtilsByDruid.getConnection();
  System.out.println(connection.getClass()); //运行类型com.alibaba.druid.pool.DruidPooledConnection
 preparedStatement = connection.prepareStatement(sql);
 preparedStatement.setInt(1, 1);//给?号赋值
 //执行, 得到结果集
set = preparedStatement.executeQuery();

 //遍历该结果集
while (set.next()) {
 int id = set.getInt("id");
 String name = set.getString("name");//getName()
 String sex = set.getString("sex");//getSex()
 Date borndate = set.getDate("borndate");
 String phone = set.getString("phone");
 System.out.println(id + "\t" + name + "\t" + sex + "\t" + borndate + "\t" + phone);
 }
 } catch (SQLException e) {
     e.printStackTrace();
 } finally {
 //关闭资源
    JDBCUtilsByDruid.close(set, preparedStatement, connection);
 } }

 //使用土方法来解决ResultSet=封装=>Arraylist
 @Test
 public ArrayList<Actor> testSelectToArrayList() {
 System.out.println("使用 druid 方式完成");
 //1. 得到连接
Connection connection = null;
 //2. 组织一个sql
 String sql = "select * from actor where id >= ?";
 PreparedStatement preparedStatement = null;
 ResultSet set = null;
 ArrayList<Actor> list = new ArrayList<>();//创建 ArrayList 对象,存放 actor 对象
//3. 创建PreparedStatement 对象

try {
 connection = JDBCUtilsByDruid.getConnection();
 System.out.println(connection.getClass());//运行类型 com.alibaba.druid.pool.DruidPooledConnection
 preparedStatement = connection.prepareStatement(sql);
 preparedStatement.setInt(1, 1);//给?号赋值
//执行, 得到结果集
set = preparedStatement.executeQuery();
 //遍历该结果集
while (set.next()) {
 int id = set.getInt("id");
 String name = set.getString("name");//getName()
 String sex = set.getString("sex");//getSex()
 Date borndate = set.getDate("borndate");
 String phone = set.getString("phone");
 //把得到的resultset 的记录,封装到 Actor对象,放入到list集合
list.add(new Actor(id, name, sex, borndate, phone));
 }

 System.out.println("list 集合数据=" + list);
 for(Actor actor : list) {
 System.out.println("id=" + actor.getId() + "\t" + actor.getName());
 }

 } catch (SQLException e) {
 e.printStackTrace();
} finally {
 //关闭资源
JDBCUtilsByDruid.close(set, preparedStatement, connection);
 }
 //因为ArrayList 和 connection 没有任何关联,所以该集合可以复用.
 return list;
 } }

1.12 Apache—DBUtils

1. 关闭connection后,resultSet结果集无法使用
2. resultSet 不利于数据的管理

用土方法来解决:

//使用土方法来解决ResultSet=封装=>Arraylist
 @Test
 public ArrayList<Actor> testSelectToArrayList() {
 System.out.println("使用 druid 方式完成");
 //1. 得到连接
Connection connection = null;
 //2. 组织一个sql
 String sql = "select * from actor where id >= ?";
 PreparedStatement preparedStatement = null;
 ResultSet set = null;
 ArrayList<Actor> list = new ArrayList<>();//创建 ArrayList 对象,存放 actor 对象
//3. 创建PreparedStatement 对象
try {
 connection = JDBCUtilsByDruid.getConnection();
System.out.println(connection.getClass());//运行类型 com.alibaba.druid.pool.DruidPooledConnection
 preparedStatement = connection.prepareStatement(sql);
 preparedStatement.setInt(1, 1);//给?号赋值
 //执行, 得到结果集
set = preparedStatement.executeQuery();
 //遍历该结果集
while (set.next()) {
 int id = set.getInt("id");
String name = set.getString("name");//getName()
 String sex = set.getString("sex");//getSex()
 Date borndate = set.getDate("borndate");
 String phone = set.getString("phone");
 //把得到的resultset 的记录,封装到 Actor对象,放入到list集合
list.add(new Actor(id, name, sex, borndate, phone));
 }
 System.out.println("list 集合数据=" + list);
 for(Actor actor : list) {
 System.out.println("id=" + actor.getId() + "\t" + actor.getName());
 }
 } catch (SQLException e) {
 e.printStackTrace();
 } finally {
 //关闭资源
JDBCUtilsByDruid.close(set, preparedStatement, connection);
}
//因为ArrayList 和 connection 没有任何关联,所以该集合可以复用.
 return list;
 }

1.12.1 基本介绍

1. commons-dbutils是 Apache组织提供的一个开源JDBC工具类库,它是对JDBC的封装,使用dbutils能极大简化jdbc编码的工作量。
DbUtils类
1. QueryRunner类:该类封装了SQL的执行,是线程安全的。可以实现增、删、改、查、批处理
2. 使用QueryRunner类实现查询
3. ResultSetHandler接口:该接口用于处理java.sql.ResultSet,将数据按要求转换为另一种形

1.12.2 实例:

        使用DBUtils+数据连接池(德鲁伊)方式,完成对表actor的crud

 import org.apache.commons.dbutils.QueryRunner;
 import org.apache.commons.dbutils.handlers.BeanHandler;
 import org.apache.commons.dbutils.handlers.BeanListHandler;
 import org.apache.commons.dbutils.handlers.ScalarHandler;
 import org.junit.jupiter.api.Test;
 import java.sql.*;
 import java.util.ArrayList;
 import java.util.List;

@SuppressWarnings({"all"})
 public class DBUtils_USE {
     //使用apache-DBUtils 工具类 + druid 完成对表的crud操作
    @Test
     public void testQueryMany() throws SQLException { //返回结果是多行的情况
    //1. 得到 连接 (druid)
     Connection connection = JDBCUtilsByDruid.getConnection();
     //2. 使用 DBUtils 类和接口 , 先引入DBUtils 相关的jar, 加入到本Project
     //3. 创建 QueryRunner
     QueryRunner queryRunner = new QueryRunner();
    //4. 就可以执行相关的方法,返回ArrayList 结果集
    //String sql = "select * from actor where id >= ?";
     // 注意:sql 语句也可以查询部分列
    String sql = "select id, name from actor where id >= ?";
// 解读
//(1) query 方法就是执行sql 语句,得到resultset---封装到-->ArrayList 集合中
//(2) 返回集合
//(3) connection: 连接
//(4) sql : 执行的 sql 语句
//(5) new BeanListHandler<>(Actor.class): 在将 resultset-> Actor 对象-> 封装到 ArrayList
// 底层使用反射机制 去获取Actor 类的属性,然后进行封装
//(6) 1 就是给 sql 语句中的? 赋值,可以有多个值,因为是可变参数Object...params
 //(7) 底层得到的resultset ,会在 query 关闭, 关闭PreparedStatment
 /**
  * 分析 queryRunner.query 方法:
  * public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
 * PreparedStatement stmt = null;//定义 PreparedStatement
 * ResultSet rs = null;//接收返回的 ResultSet
 * Object result = null;//返回 ArrayList
try {
 stmt = this.prepareStatement(conn, sql);//创建 PreparedStatement
 this.fillStatement(stmt, params);//对 sql 进行 ? 赋值
rs = this.wrap(stmt.executeQuery());//执行 sql,返回 resultset
 result = rsh.handle(rs);//返回的 resultset--> arrayList[result] [使用到反射,对传入 class 对象处理]
} catch (SQLException var33) {
 this.rethrow(var33, sql, params);
 } finally {
 try {
 this.close(rs);//关闭 resultset
} finally {
 this.close((Statement)stmt);//关闭 preparedstatement 对象
}
 }
 return result;
 */

List<Actor> list =  queryRunner.query(connection, sql, new BeanListHandler<>(Actor.class), 1);
 System.out.println("输出集合的信息");
     for (Actor actor : list) {
         System.out.print(actor);
 }

 //释放资源
    JDBCUtilsByDruid.close(null, null, connection);
 }
 //演示 apache-dbutils + druid 完成 返回的结果是单行记录(单个对象)
 @Test
 public void testQuerySingle() throws SQLException {
     //1. 得到 连接 (druid)
Connection connection = JDBCUtilsByDruid.getConnection();
 //2. 使用 DBUtils 类和接口 , 先引入DBUtils 相关的jar, 加入到本Project
 //3. 创建 QueryRunner
 QueryRunner queryRunner = new QueryRunner();
 //4. 就可以执行相关的方法,返回单个对象
String sql = "select * from actor where id = ?";
 // 解读
// 因为我们返回的单行记录<--->单个对象 , 使用的Hander 是 BeanHandler
 Actor actor = queryRunner.query(connection, sql, new BeanHandler<>(Actor.class), 10);
 System.out.println(actor);
 // 释放资源
    JDBCUtilsByDruid.close(null, null, connection);
 }
 //演示apache-dbutils + druid 完成查询结果是单行单列-返回的就是object
 @Test
 public void testScalar() throws SQLException {
     //1. 得到 连接 (druid)
    Connection connection = JDBCUtilsByDruid.getConnection();
 //2. 使用 DBUtils 类和接口 , 先引入DBUtils 相关的jar, 加入到本Project
 //3. 创建 QueryRunner
     QueryRunner queryRunner = new QueryRunner();
//4. 就可以执行相关的方法,返回单行单列 , 返回的就是Object
 String sql = "select name from actor where id = ?";
 //解读: 因为返回的是一个对象, 使用的handler 就是 ScalarHandler
 Object obj = queryRunner.query(connection, sql, new ScalarHandler(), 4);
 System.out.println(obj);
 // 释放资源
JDBCUtilsByDruid.close(null, null, connection);
 }
 //演示apache-dbutils + druid 完成 dml (update, insert ,delete)
 @Test
 public void testDML() throws SQLException {
 //1. 得到 连接 (druid)
 Connection connection = JDBCUtilsByDruid.getConnection();
 //2. 使用 DBUtils 类和接口 , 先引入DBUtils 相关的jar, 加入到本Project
 //3. 创建 QueryRunner
 QueryRunner queryRunner = new QueryRunner();
 //4. 这里组织sql 完成 update,insert delete
 //String sql = "update actor set name = ? where id = ?";
 //String sql = "insert into actor values(null, ?, ?, ?, ?)";
 String sql = "delete from actor where id = ?";
 //解读
//(1) 执行 dml 操作是 queryRunner.update()
 //(2) 返回的值是受影响的行数 (affected: 受影响)
 //int affectedRow = queryRunner.update(connection, sql, "林青霞", "女", "1966-10-10", "116");
 int affectedRow = queryRunner.update(connection, sql, 1000 );
 System.out.println(affectedRow > 0 ? "执行成功" : "执行没有影响到表");
 // 释放资源
JDBCUtilsByDruid.close(null, null, connection);
 } }

1.12.3 表和 JavaBean 的类型映射关系

1.13 DAO 和增删改查通用方法-BasicDao

apache-dbutils+ Druid简化了JDBC开发,但还有不足:
1. SQL语句是固定,不能通过参数传入,通用性不好,需要进行改进,更方便执行增删改查
2. 对于select 操作,如果有返回值,返回类型不能固定,需要使用泛型
3. 将来的表很多,业务需求复杂,不可能只靠一个Java类完成

1.13.1 基本说明

1. DAO: data access object数据访问对象
2. 这样的通用类,称为BasicDao,是专门和数据库交互的,即完成对数据库(表)的crud操作。
3. 在BaiscDao的基础上,实现一张表对应一个Dao,更好的完成功能,比如 Customer表-
Customer.java类(avabean) 

1.13.2 BasicDAO应用实例

完成一个简单设计com.zakedu.dao_
1. com.zakedu.dao_.utils //工具类
2. com.zakedu.dao_.domain  // javabean
3. com.zakedu.dao_.dao //存放XxxDAO 和BasicDAO
4. com.zakedu.dao_.test //写测试类

package com.zakedu.dao_.domain;
 import java.util.Date;
 /**
  *Actor对象和actor表的记录对应
 */
public class Actor{  //Javabean,POJO,Domain对象
    private Integer id;
    private String name;
    private String sex;
    private Date borndate;
    private String phone;

    public Actor() { //一定要给一个无参构造器[反射需要]
 }

 public Actor(Integer id, String name, String sex, Date borndate, String phone) {
     this.id = id;
     this.name = name;
     this.sex = sex;
     this.borndate = borndate;
     this.phone = phone;
 }

public Integer getId() {
     return id;
 }

public void setId(Integer id) {
     this.id = id;
 }

 public String getName() {
     return name;
}

 public void setName(String name) {
     this.name = name;
 }

 public String getSex() {
     return sex;
 }

 public void setSex(String sex) {
     this.sex = sex;
 }

 public Date getBorndate() {
     return borndate;
 }

 public void setBorndate(Date borndate) {
     this.borndate = borndate;
 }

 public String getPhone() {
     return phone;
 }

public void setPhone(Stringphone){
     this.phone=phone;
 }

 @Override
 public String toString(){
     return"\nActor{"+
         "id="+id+
         ",name='"+name+'\''+
         ",sex='"+sex+'\''+
         ",borndate="+borndate+
         ",phone='"+phone+'\''+
         '}';
 } }
 package com.zakedu.dao_.utils;
 import com.alibaba.druid.pool.DruidDataSourceFactory;
 import javax.sql.DataSource;
 import java.io.FileInputStream;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Properties;
 /**
  * 基于druid数据库连接池的工具类
*/
public class JDBCUtilsByDruid {
 private static DataSource ds;
     //在静态代码块完成 ds初始化
    static {
         Properties properties = new Properties();
     try {
        properties.load(new FileInputStream("src\\druid.properties"));
         ds = DruidDataSourceFactory.createDataSource(properties);
     } catch (Exception e) {
         e.printStackTrace();
 } }

 //编写getConnection 方法
public static Connection getConnection() throws SQLException {
     return ds.getConnection();
}

//关闭连接, 老师再次强调: 在数据库连接池技术中,close 不是真的断掉连接
//而是把使用的Connection对象放回连接池
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
     try {
         if (resultSet != null) {
             resultSet.close();
         }
         if (statement != null) {
             statement.close();
         }
         if (connection != null) {
             connection.close();
         }
         } catch (SQLException e) {
             throw new RuntimeException(e);
 } } }
 package com.zakedu.dao_.dao;
 import com.zakedu.dao_.utils.JDBCUtilsByDruid;
 import org.apache.commons.dbutils.QueryRunner;
 import org.apache.commons.dbutils.handlers.BeanHandler;
 import org.apache.commons.dbutils.handlers.BeanListHandler;
 import org.apache.commons.dbutils.handlers.ScalarHandler;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.List;
 /**
   * 开发BasicDAO, 是其他DAO的父类, 使用到apache-dbutils
 */
 public class BasicDAO <T> { //泛型指定具体类型
    private QueryRunner qr = new QueryRunner();
     //开发通用的dml方法, 针对任意的表
    public int update(String sql, Object... parameters) {
         Connection connection = null;
         try {
             connection = JDBCUtilsByDruid.getConnection();
             int update = qr.update(connection, sql, parameters);
             return update;
        } catch (SQLException e) {
         throw newRuntimeException(e); //将编译异常->运行异常 ,抛出
        } finally {
         JDBCUtilsByDruid.close(null, null, connection);
     } }

 //返回多个对象(即查询的结果是多行), 针对任意表

/**
 *
 * @param sql sql 语句,可以有 ?
 * @param clazz 传入一个类的Class对象 比如 Actor.class
 * @param parameters 传入 ? 的具体的值,可以是多个
* @return 根据Actor.class 返回对应的 ArrayList 集合
*/

public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {
     Connection connection = null;
     try {
         connection = JDBCUtilsByDruid.getConnection();
         return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
    } catch (SQLException e) {
         throw newRuntimeException(e); //将编译异常->运行异常 ,抛出
    } finally {
         JDBCUtilsByDruid.close(null, null, connection);
     } }

 //查询单行结果 的通用方法
public T querySingle(String sql, Class<T> clazz, Object... parameters) {
     Connection connection = null;
     try {
         connection = JDBCUtilsByDruid.getConnection();
         return qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
         } catch (SQLException e) {
             throw newRuntimeException(e); //将编译异常->运行异常 ,抛出
         } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }}

 //查询单行单列的方法,即返回单值的方法
public Object queryScalar(String sql, Object... parameters) {
     Connection connection = null;
try{
     connection=JDBCUtilsByDruid.getConnection();
     return qr.query(connection,sql,newScalarHandler(),parameters);
 }catch(SQLExceptione){
     throw newRuntimeException(e);//将编译异常->运行异常,抛出
}finally{
     JDBCUtilsByDruid.close(null,null,connection);
 } } }
 package com.zakedu.dao_.dao;
 importcom.zakedu.dao_.domain.Actor;

 public class ActorDAO extends BasicDAO<Actor>{
     //1.就有BasicDAO的方法
    //2.根据业务需求,可以编写特有的方法.
 }
package com.zakedu.dao_.test;
 import com.zakedu.dao_.dao.ActorDAO;
 import com.zakedu.dao_.domain.Actor;
 import org.junit.jupiter.api.Test;
 import java.util.List;

public class TestDAO {
 //测试ActorDAO 对actor 表crud 操作
    @Test
     public void testActorDAO() {
     ActorDAO actorDAO = newActorDAO();
     //1. 查询
    List<Actor> actors = actorDAO.queryMulti("select * from actor where id >= ?", Actor.class, 1);
     System.out.println("===查询结果===");
     for (Actor actor : actors) {
         System.out.println(actor);
 }
//2.查询单行记录
Actor actor = actor DAO.querySingle("select * from actor whereid=?",Actor.class,6);
 System.out.println("====查询单行结果====");
 System.out.println(actor);
 //3.查询单行单列
Object o = actor DAO.queryScalar("select name from actor where id=?",6);
 System.out.println("====查询单行单列值===");
 System.out.println(o);
 //4.dml操作insert,update,delete
 int update = actor DAO.update("insert into actor values(null,?,?,?,?)","张无忌","男","2000-11-11","999");
 System.out.println(update>0?"执行成功":"执行没有影响表");
 } }
  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值