Chiriri's blog Chiriri's blog
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)

Iekr

苦逼后端开发
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)
  • JavaSE

    • Java简介
    • 基础语法
    • 数据类型
    • 变量
    • 运算符
    • 输入输出
    • 流程控制
    • 循环语句
    • idea中的辅助键
    • 数组
    • 方法
    • 面向对象基础
    • 字符串
    • ArrayList集合
    • 继承
    • 修饰符
    • 多态
    • 抽象
    • 接口
    • 类名作为形参和返回值
    • 内部类
    • Api
    • 异常
    • 集合
    • 泛型
    • Set集合和比较器
    • 树
    • 哈希
    • 可变参数
    • 创建不可变的集合
    • Stream流
    • 方法引用
    • File
    • 多线程
    • 多线程高级
    • 网络编程
    • 类加载器
    • 反射
    • XML
    • 枚举
    • 注解
    • 单元测试
    • 日志
    • HTTP协议
    • Servlet
    • 请求对象
    • 响应对象
    • Cookie
    • Session
    • JSP
    • Listener
    • JDBC
      • DriverManager 连接
      • Connection 连接对象
      • Statement 执行语句
      • ResultSet 结果
      • 工具类
      • SQL注入攻击
      • 数据库连接池
      • 自定义数据库连接池
        • 归还连接
        • 继承方式
        • 装饰设计模式
        • 适配器设计模式
      • 动态代理
      • 以动态代理 规划连接
      • 开源数据库连接池
        • C3P0
        • Druid
      • 自定义JDBC框架
  • JavaEE

  • Linux

  • MySQL

  • NoSQL

  • Python

  • Python模块

  • 机器学习

  • 设计模式

  • 传智健康

  • 畅购商城

  • 博客项目

  • JVM

  • JUC

  • Golang

  • Kubernetes

  • 硅谷课堂

  • C

  • 源码

  • 神领物流

  • RocketMQ

  • 短链平台

  • 后端
  • JavaSE
Iekr
2021-08-13
目录

JDBC

# JDBC

  • 导入 mysql-connector-java.jar 包

  • 注册驱动

    • Class.forName("com.mysql.jdbc.Driver");
      
      1
  • 建立连接

    •  Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql", "root", "123456");
      
      1
  • 获取对象

    • Statement stat = con.createStatement();
      
      1
  • 执行 sql 语句

    • String sql ="select * from help_relation";
      ResultSet result = stat.executeQuery(sql);
      
      1
      2
  • 处理结果

    •  while (result.next()){
        	System.out.println(result.getInt("help_topic_id")+"\t"+ result.getString("help_keyword_id"));
      }
      
      1
      2
      3
  • 关闭连接

    • con.close();
      stat.close();
      
      1
      2

# DriverManager 连接

DriverManager 驱动管理对象

  • 注册驱动 registerDriver (Driver river) 注册给定的驱动 在上面注册中我们使用 class.forname 使用了一下 Driver 这个类,默认被使用就注册驱动 必须是 mysql5 之后才可以省略这个注册
  • 获取数据连接 getConnection (url , user , password) url 为 jdbc:mysql://ip: 端口 / 库名

# Connection 连接对象

Connection 数据库连接对象

  • 获取普通执行者对象 createStatement ()
  • 获取预编译执行者对象 prepareStatement (String sql)
  • 开启事务 setAutoCommit (boolean autoCommit); 参数为 false 则开启事务
  • 提交事务 commit ()
  • 回滚事务 rollback ()
  • 释放资源 close ();

# Statement 执行语句

Statement 执行 sql 语句的对象

  • 增删改 executeUpdate (String sql) 返回一个 int 影响的行数 可以执行 insert、update、delete 语句
  • 查询 executeQuery(String sql) 返回值 resultset 对象 封装查询的结果 可以执行 select 语句
  • 释放资源 close ()

# ResultSet 结果

ResultSet 结果集对象

  • 判断结果是否还有数据 next () 有数据返回 ture, 并索引向下移动一行
  • get 数据类型 ("列名") 返回指定列的指定数据类型结果 如 getInt () getString ()
  • 释放资源 close ()

# 工具类

在 src 下创建 config.properties 文件

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mysql
username=root
password=123456
1
2
3
4
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private JDBCUtils() {
    }

    //2.声明需要的配置变量
    private static String driverClass;
    private static String url;
    private static String usename;
    private static String password;
    private static Connection con;

    //3.提供静态代码块 读取配置文件为变量赋值
    static {
        try {
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties");
            Properties prop = new Properties();
            prop.load(is);

            driverClass = prop.getProperty("driverClass");
            url = prop.getProperty("url");
            usename = prop.getProperty("usename");
            password = prop.getProperty("password");

            //注册驱动

            Class.forName(driverClass);


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static Connection getConnection() {
        try {
            con = DriverManager.getConnection(url, usename, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return con;
    }

    public static void close(Connection con, Statement stat, ResultSet rs) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    public static void close(Connection con, Statement stat) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


    }
}
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

其实就是封装一个连接和关闭工具类方法

# SQL 注入攻击

在之前我们使用 Statement 对象执行 sql 语句并且 sql 语句是拼接字符串而成的 不安全

为了解决这个问题我们使用 PreparedStatement 预编译执行者对象 并且参数以?形式作为占位符

为占位符 问号 赋值的方法: setXxx (参数 1, 参数 2)

  • Xxx 为数据的类型
  • 参数 1 为?的下标 从 1 开始
  • 参数 2 为?的实际参数

内置 setObject ()

Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql", "root", "123456");
String sql = "delete from  user where name = ?";
PreparedStatement pstm = con.prepareStatement(sql);
pstm.setString(1,"张三");
1
2
3
4

执行 SQL 语句

  • 执行 inser update delete 语句 executeUpdate ();
  • 执行 select 语句 executeQuery ();

# 数据库连接池

数据库连接池负责分配 管理 释放数据库连接 它允许程序重复使用一个现有的数据库连接 而不是重新建立一个

# 自定义数据库连接池

  • 继承 DataSource 接口
  • 准备一个容器 用于保存多个连接对象
  • 定义静态代码块 获取多个连接对象并放入容器中
  • 重写 getConnection () 方法 用于获取一个连接对象
  • 定义一个 getSize 方法 获取连接池容器的大小
// 准备一个容器  用于保存多个连接对象
private static List<Connection> pool = Collections.synchronizedList(new ArrayList<>());

//定义静态代码块  获取多个连接对象并放入容器中
static {
    for(int i=1;i<=10;i++){
        Connection con = JDBCUtils.getConnection();
        pool.add(con);

    }
}

//重写getConnection()方法  用于获取一个连接对象
@Override
public Connection getConnection() throws SQLException {
    if(pool.size() > 0 ){
        Connection con = pool.remove(0);
        return con;
    }else {
        throw new RuntimeException("连接数据已用尽");
    }
}

//定义一个getSize方法 获取连接池容器的大小
public int getSize(){
    return pool.size();
}
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

# 归还连接

把连接对象归还给连接池

# 继承方式

打印通过连接池创建的连对象的 class

System.out.println(con.getClass());
//class com.mysql.cj.jdbc.ConnectionImpl
1
2

接下定义一个类 继承 ConnectionImpl 类

public class return01 extends ConnectionImpl {
    private Connection con;
    private List<Connection> pool;


    public return01(HostInfo hostInfo, Connection con, List<Connection> pool) throws SQLException {
        super(hostInfo);
        this.con = con;
        this.pool = pool;
    }

    // 重写close方法 将连接对象 重写归还容器中
    @Override
    public void close() throws SQLException {
        pool.add(con);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

但 DriverManager 获取的还是 ConnectionImpl 这个对象,继承这种方法行不通的

# 装饰设计模式

  • 实现 Connection 接口
// 定义连接对象和连接容器的成员变量
    private Connection con;
    private List<Connection> pool;

    //通过有参构造方法为成员变量赋值
    public return02(Connection con,List<Connection> pool){
        this.con=con;
        this.pool=pool;
    }

    // 重写close方法
    @Override
    public void close() throws SQLException {
        pool.add(con);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

其他方法调用原本方法

装饰设计模式就是 重写需要的方法 其他方法则调用原来类的方法

# 适配器设计模式

适配器设计模式 实现接口的所有方法 除了需要的方法 然后再建一个类 继承该实现类 再重新未实现的方法 (使用中间件)

  • 定义适配类 实现 Connection 接口 定义为一个抽象类
  • 定义连接对象的成员变量 Connection con
  • 通过有参构造方法赋值
  • 重写所有抽象方法 除了 close
  • 继承适配类 成员变量 Connection 和 连接容器
  • 通过有参构造方法赋值
  • 重写 close 方法

# 动态代理

在不改变目标对象方法的情况下对方法进行增强

被代理对象:真实的对象

代理对象:内容中的一个对象

代理对象必须和被代理对象实现相同的接口

        Student st = new Student();

    /*
        参数1 为 类加载器   和被代理对象使用相同的类加载器
        参数2 为 接口类型Class数组  和被代理对象使用相同的接口
        参数3 为 代理规则 完成代理增强的功能
     */
        StudentInterface proxySt =(StudentInterface) Proxy.newProxyInstance(st.getClass().getClassLoader(), new Class[]{StudentInterface.class}, new InvocationHandler() {
            /*
                Student类中所有的方法都经过invoke方法
                我们通过对方法继续判断
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //method为类执行某个方法   args为方法的实际参数
                if (method.getName().equals("study")) {
                    // 判断为某个方法
                    System.out.println("hello world");
                    return null;
                } else {
                    // 如不是则按原本方法执行
                    return method.invoke(st,args);
                }
            }
        });

        // 代理对象执行方法
        proxySt.eat("肉");
        proxySt.study();
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

# 以动态代理 规划连接

 //重写getConnection()方法  用于获取一个连接对象
    @Override
    public Connection getConnection() throws SQLException {
        if (pool.size() > 0) {
            Connection con = pool.remove(0);

            Connection proxycon= (Connection) Proxy.newProxyInstance(con.getClass().getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if(method.getName().equals("close")){
                        pool.add(con);
                        return null;
                    }else {
                        return method.invoke(con,args);
                    }
                }
            });
            return proxycon;
        } else {
            throw new RuntimeException("连接数据已用尽");
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 开源数据库连接池

# C3P0

  1. 导入 jar 包

  2. 导入配置文件到 src 目录下

  3. 创建 C3P0 连接池对象

    DataSource dataSource=new ComboPooledDataSource();
    
    1
  4. 获取数据库连接并使用

    Connection con = dataSource.getConnection();
    
    1

配置文件会自动加载 必须命名为 c3p0-config.xml 或 c3p0-config.properties

# Druid

  • 导入 jar 包

  • 通过 properties 集合 加载配置文件

    InputStream is = druiddemo01.class.getClassLoader().getResourceAsStream("笔记/jdbc/src/druid.properties");
    Properties prop =new Properties();
    prop.load(is);
    
    1
    2
    3
  • 通 Druid 连接池工厂类获取连接池对象

    DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    
    1
  • 通过连接池获取数据库对象

    Connection con = dataSource.getConnection();
    
    1

# 自定义 JDBC 框架

  • DataBaseMetaData 数据库的源信息
    • getDatabaseProductName () 获取数据库产品的名称
    • getDatabaseProductVersion () 获取数据库产品的版本号
  • getParameterMetaData () 参数的源信息 预编译对象获取方法
    • getParameterCount () 获取 sql 语句中参数的个数
  • getMetaData () 获取结果集的源信息 通过结果集对象获取
    • getColumnCount () 获取列的总数
    • getColumnName (int i) 获取列名

mysdq

编辑 (opens new window)
上次更新: 2025/01/01, 10:09:39
Listener
MyBatis

← Listener MyBatis→

最近更新
01
k8s
06-06
02
进程与线程
03-04
03
计算机操作系统概述
02-26
更多文章>
Theme by Vdoing | Copyright © 2022-2025 Iekr | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式