JDBC入门级知识点

JDBC简介

概念

JDBC就是使用Java语言操作关系型数据库的一套API

全称 Java Data Base Connectivity Java数据库连接

本质

定义的一套操作所有关系数据库的规则,即接口

请添加图片描述

快速入门

步骤

请添加图片描述

API详解

DriverManager

驱动管理类 作用:

  1. 注册驱动
  2. 获取数据库连接
Class.forName("com.mysql.jdbc.Driver");

此句可省略。
在这里插入图片描述

Connection

数据库连接对象 作用:

  1. 获取执行SQL的对象
  2. 管理事务
  • 普通执行SQL对象
Statement createStatement();
  • 预编译SQL的执行SQL对象:防止SQL注入
PreparedStatement prepareStatement(sql);
  • 执行存储过程的对象
CallableStatement prepareCall(sql);

请添加图片描述

Statement

作用:执行sql语句

executeUpdate(sql);  //执行DML、DDL语句  返回int类型
//返回值:(1)DML语句影响的行数    (2)DDL语句执行后,执行成功也可能返回0
executeQuery(sql);  //执行DQL语句  返回ResultSet类型
//返回值:(1)ResultSet jie'guo'ji

执行DDL语句的返回值可能是0

ResultSet

结果集对象:封装了DDL查询语句的结果

stmt.executeQuery(sql);   //执行DQL语句,返回ResultSet对象

在这里插入图片描述

使用步骤:

  1. 游标向下移动一行,并判断改行是否有数据:next()
  2. 获取数据:getXxx(参数)
//循环判断游标是否是最后一行末尾
while(rs.next()){
	rs.getXxx(参数);
}

在这里插入图片描述

  1. 定义一个实体类Account
  2. 查询数据,封装到Account对象中
  3. 讲数据存入ArrayList集合中

!!!

PreparedStatement

作用:

  1. 预编译SQL语句并执行:预防SQL注入问题

SQL注入:通过操作输入来修改事先定义好的SQL语句,用以达到执行代码对服务器进行攻击的方法。
在这里插入图片描述
原理:
在这里插入图片描述
好处:

  1. 预编译SQL,性能更高。
  2. 防止SQL注入,将敏感字符进行转义。
    在这里插入图片描述

数据库连接池

简介

数据库连接池是一个容器,负责分配,管理数据库连接(connection)。

它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个。

释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏

好处

  1. 资源重用
  2. 提升系统响应时间
  3. 避免数据库连接遗漏。

在这里插入图片描述

数据库连接池实现

标准接口: DataSource

官方提供的数据库连接池标准接口,由第三方组织实现此接口。

Connection     getConnection();

常见的数据库连接池:

  1. DBCP
  2. C3P0
  3. Druid:(德鲁伊) 阿里巴巴开源的数据库连接池项目,功能强大,性能优秀,是Java语言最好的数据库连接池之一
    在这里插入图片描述

练习

在这里插入图片描述

准备环境

  1. [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HqrIdYCL-1664105302356)(2JDBC.assets/image-20220711153722784.png)]

  2. 查询[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g3LWd7EI-1664105302357)(2JDBC.assets/image-20220711161053695.png)]

  3. 添加[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4KSYfhum-1664105302357)(2JDBC.assets/image-20220711170646571.png)]

  4. 修改[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x6mym6Tr-1664105302358)(2JDBC.assets/image-20220711193121424.png)]

  5. 删除[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yVDQTI6g-1664105302359)(2JDBC.assets/image-20220711195740375.png)]

 //查询所有数据
    @Test
    public void testSelectAll() throws Exception {
        //加载配置文件
        Properties prop=new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        //获取连接池对象

        DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
        //获取数据库连接Connection
        Connection connection=dataSource.getConnection();
        //定义sql
        String sql="select * from tb_brand";
        //获取
        PreparedStatement pstmt = connection.prepareStatement(sql);
        //执行SQL
        ResultSet rs=pstmt.executeQuery();
        //创建集合
        List<Brand> list=new ArrayList<>();

        //处理结果,遍历所有数据
        Brand brand=null;
        while (rs.next()){
            int id= rs.getInt("id");
            String brandName= rs.getString("brand_name");
            String companyName = rs.getString("company_name");
            int ordered = rs.getInt("ordered");
            String description = rs.getString("description");
            int status = rs.getInt("status");

            //封装对象
            brand=new Brand();
            brand.setId(id);
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);



            //存入集合
            list.add(brand);
        }
        System.out.println(list);
        //释放资源
        rs.close();
        pstmt.close();
        connection.close();
    }

    //添加
    @Test
    public void testAdd() throws Exception {
        //模拟接收数据
        String brandName="香飘飘";
        String companyName="香飘飘";
        int ordered=1;
        String description="绕地球一圈";
        int status=1;

        //加载配置文件
        Properties prop=new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        //获取连接池对象

        DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
        //获取数据库连接Connection
        Connection connection=dataSource.getConnection();
        //定义sql
        String sql="insert into tb_brand(brand_name, company_name, ordered, description, status) VALUES(?,?,?,?,?)";
        //获取
        PreparedStatement pstmt = connection.prepareStatement(sql);

        //设置参数
        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);
        pstmt.setInt(5,status);

        //执行SQL
        int count= pstmt.executeUpdate();
        //处理结果
        if (count>0){
            System.out.println(true);
        }else {
            System.out.println(false);
        }

        //释放资源
        pstmt.close();
        connection.close();
    }
    //修改
    @Test
    public void testUpate() throws Exception {
        //模拟接收数据
        String brandName="香飘飘";
        String companyName="香飘飘";
        int ordered=1;
        String description="绕地球三圈";
        int status=1;
        int id=4;

        //加载配置文件
        Properties prop=new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        //获取连接池对象

        DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
        //获取数据库连接Connection
        Connection connection=dataSource.getConnection();
        //定义sql
        String sql="update tb_brand\n"+
                "      set brand_name=?,\n"+
                "      company_name  =?,\n"+
                "      ordered        =?,\n"+
                "      description   =?,\n"+
                "      status        =?\n  "+
                "    where id        = ?";
        //获取
        PreparedStatement pstmt = connection.prepareStatement(sql);
        //设置参数
        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);
        pstmt.setInt(5,status);
        pstmt.setInt(6,id);

        //执行SQL
        int count= pstmt.executeUpdate();
        //处理结果
        if (count>0){
            System.out.println(true);
        }else {
            System.out.println(false);
        }

        //释放资源
        pstmt.close();
        connection.close();
    }

    //删除
    @Test
    public void testDelete() throws Exception {
        //模拟接收数据

        int id=4;

        //加载配置文件
        Properties prop=new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        //获取连接池对象

        DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
        //获取数据库连接Connection
        Connection connection=dataSource.getConnection();
        //定义sql
        String sql="delete from tb_brand where id=?";
        //获取
        PreparedStatement pstmt = connection.prepareStatement(sql);
        //设置参数
        pstmt.setInt(1,id);

        //执行SQL
        int count= pstmt.executeUpdate();
        //处理结果
        if (count>0){
            System.out.println(true);
        }else {
            System.out.println(false);
        }

        //释放资源
        pstmt.close();
        connection.close();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值