• JDBC技术(一)——一个简单的JDBC测试


    什么是JDBC

    JDBC 指 Java 数据库连接,是一种标准Java应用编程接口( JAVA API),用来连接 Java 编程语言和广泛的数据库。

    从根本上来说,JDBC 是一种规范,它提供了一套完整的接口,允许便携式访问到底层数据库,因此可以用 Java 编写不同类型的可执行文件

    JDBC架构

    JDBC 的 API 支持两层和三层处理模式进行数据库访问,但一般的 JDBC 架构由两层处理模式组成:

    • JDBC API: 使用驱动程序管理器和数据库特定的驱动程序来提供异构(heterogeneous)数据库的透明连接。
    • JDBC Driver API: JDBC 驱动程序管理器可确保正确的驱动程序来访问每个数据源。该驱动程序管理器能够支持连接到多个异构数据库的多个并发的驱动程序。
      在这里插入图片描述

    常用的JDBC组件

    • DriverManager :这个类管理一系列数据库驱动程序。匹配连接使用通信子协议从 JAVA 应用程序中请求合适的数据库驱动程序。识别 JDBC 下某个子协议的第一驱动程序将被用于建立数据库连接。
    • Driver : 这个接口处理与数据库服务器的通信。你将很少直接与驱动程序互动。相反,你使用 DriverManager 中的对象,它管理此类型的对象。它也抽象与驱动程序对象工作相关的详细信息。
    • Connection : 此接口具有接触数据库的所有方法。该连接对象表示通信上下文,即,所有与数据库的通信仅通过这个连接对象进行。
    • Statement : 使用创建于这个接口的对象将 SQL 语句提交到数据库。除了执行存储过程以外,一些派生的接口也接受参数。
    • ResultSet : 在你使用语句对象执行 SQL 查询后,这些对象保存从数据获得的数据。它作为一个迭代器,让您可以通过它的数据来移动。
    • SQLException : 这个类处理发生在数据库应用程序的任何错误。

    前期准备

    新建java项目,创建lib目录并导入mysql驱动
    在这里插入图片描述

    项目结构

    在这里插入图片描述

    jdbc的编写流程

    • 导入mysql驱动,注册驱动
    • 创建与数据库之间的连接(tcp)
    • 封装sql并传输给mysql
    • 数据库响应并处理
    • 释放资源关闭连接

    jdbc的代码编写

    实体类

    public class Emp1 {
        private Integer empno;
        private String ename;
        private String job;
        private Integer mgr;
        private String hiredate;
        private BigDecimal sal;
        private BigDecimal comm;
        private Integer deptno;
    
        public Emp1() {
        }
    
        public Emp1(Integer empno, String ename, String job, Integer mgr, String hiredate, BigDecimal sal, BigDecimal comm, Integer deptno) {
            this.empno = empno;
            this.ename = ename;
            this.job = job;
            this.mgr = mgr;
            this.hiredate = hiredate;
            this.sal = sal;
            this.comm = comm;
            this.deptno = deptno;
        }
    
        @Override
        public String toString() {
            return "Emp{" +
                    "empno=" + empno +
                    ", ename='" + ename + '\'' +
                    ", job='" + job + '\'' +
                    ", mgr=" + mgr +
                    ", hiredate=" + hiredate +
                    ", sal=" + sal +
                    ", comm=" + comm +
                    ", deptno=" + deptno +
                    '}';
        }
    
        public Integer getDeptno() {
            return deptno;
        }
    
        public void setDeptno(Integer deptno) {
            this.deptno = deptno;
        }
    
        public Integer getEmpno() {
            return empno;
        }
    
        public void setEmpno(Integer empno) {
            this.empno = empno;
        }
    
        public String getEname() {
            return ename;
        }
    
        public void setEname(String ename) {
            this.ename = ename;
        }
    
        public String getJob() {
            return job;
        }
    
        public void setJob(String job) {
            this.job = job;
        }
    
        public Integer getMgr() {
            return mgr;
        }
    
        public void setMgr(Integer mgr) {
            this.mgr = mgr;
        }
    
        public String getHiredate() {
            return hiredate;
        }
    
        public void setHiredate(String hiredate) {
            this.hiredate = hiredate;
        }
    
        public BigDecimal getSal() {
            return sal;
        }
    
        public void setSal(BigDecimal sal) {
            this.sal = sal;
        }
    
        public BigDecimal getComm() {
            return comm;
        }
    
        public void setComm(BigDecimal comm) {
            this.comm = comm;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    工具类

    package com.lx.jdbc01.util;
    
    import com.lx.jdbc01.entity.Emp1;
    
    import java.sql.*;
    
    /**
     * description TODO
     *
     * @author 流星
     * @date 2022/8/4 9:49
     */
    public class JDBCUtil1 {
    
        private static Connection getConnection() {
            Connection connection;
            try {
                // 1.注册驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
                // 2.建立连接
                connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT%2B8",
                        "root",
                        "123456"
                );
            } catch (ClassNotFoundException | SQLException e) {
                throw new RuntimeException("创建连接失败!!!");
            }
            return connection;
        }
    
        public static void query(String ename) throws SQLException {
            Connection connection = getConnection();
    
            /**
             * 3.封装sql并传给mysql处理
             * 用PreparedStatement而不用Statement是为了避免sql注入的问题,PreparedStatement对sql和参数进行了预处理
             * sql注入问题:比如传ename的值为 xx or 1 = 1,无论xx是什么都能查出所有的信息
             */
            String sql = "select * from emp where ename = ? ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, ename);
            ResultSet resultSet = preparedStatement.executeQuery();
    
            // 4.数据库响应并处理响应
            while (resultSet.next()) {
                int empno = resultSet.getInt("empno");
                String ename1 = resultSet.getNString("ename");
                System.out.println(empno + "***" + ename1);
            }
    
            // 5.关闭连接并释放资源
            close(connection, preparedStatement, resultSet);
        }
    
        private static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public static void insert(Emp1 emp1) throws SQLException {
            Connection connection = getConnection();
    
            String sql = "insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, emp1.getEmpno());
            preparedStatement.setString(2, emp1.getEname());
            preparedStatement.setString(3, emp1.getJob());
            preparedStatement.setInt(4, emp1.getMgr());
            preparedStatement.setString(5, emp1.getHiredate());
            preparedStatement.setBigDecimal(6, emp1.getSal());
            preparedStatement.setBigDecimal(7, emp1.getComm());
            preparedStatement.setInt(8, emp1.getDeptno());
            int count = preparedStatement.executeUpdate();
    
            if (count == 1) {
                System.out.println("插入成功");
            } else {
                System.out.println("插入失败");
            }
    
            close(connection, preparedStatement, null);
        }
    
        public static void delete(Integer empno) throws SQLException {
            Connection connection = getConnection();
    
            String sql = "delete from emp where empno = ? ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, empno);
    
            int count = preparedStatement.executeUpdate();
    
            if (count == 1) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
    
            close(connection, preparedStatement, null);
        }
    
        public static void update(Integer empno, String ename) throws SQLException {
            Connection connection = getConnection();
    
            String sql = "update emp set ename = ? where empno = ? ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, ename);
            preparedStatement.setInt(2, empno);
            int count = preparedStatement.executeUpdate();
    
            if (count == 1) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
    
            close(connection, preparedStatement, null);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    测试类

    package com.lx.jdbc01.test;
    
    import com.lx.jdbc01.entity.Emp1;
    import com.lx.jdbc01.util.JDBCUtil1;
    import org.junit.Test;
    
    import java.math.BigDecimal;
    import java.sql.SQLException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * description TODO
     *
     * @author 流星
     * @date 2022/8/4 9:50
     */
    public class MainTest1 {
    
        @Test
        public void query() throws SQLException {
            String ename="LI";
            JDBCUtil1.query(ename);
        }
    
        @Test
        public void insert() throws SQLException {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime = new Date();
            JDBCUtil1.insert(new Emp1(6666,"YUN","CLERK",7698,simpleDateFormat.format(createTime),new BigDecimal(1500),new BigDecimal(200),20));
        }
    
        @Test
        public void update() throws SQLException {
            JDBCUtil1.update(6666,"YING");
        }
    
        @Test
        public void delete() throws SQLException {
            JDBCUtil1.delete(6666);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    测试结果

    在这里插入图片描述

    附:参考资料

    JDBC介绍

  • 相关阅读:
    Java语言高级-03常用API第二部分-第1节Object类
    教你一招,轻松实现heic转换
    spring-cloud-alibaba-sentinel学习
    【数据库事务】ACID数据库四大特性理解
    【图自动编码器】基础介绍 及 基于PyTorch的图自动编码器实例代码 | MLP应用于节点级别和图级别的任务实例(附实例代码+数据集)
    Linux基础学习——shell脚本基础:bash脚本编程基础及配置文件
    安卓讲课笔记4.1 安卓按键事件
    vue3后台管理系统之顶部tabbar组件搭建
    图像处理的基本操作
    【牛客网】安全—加密和安全
  • 原文地址:https://blog.csdn.net/weixin_45974176/article/details/126221481