土办法完成set结果集封装

土办法完成set结果集封装

首先看根据数据库表的结构完成JavaBean(Dao)的编写

数据库表结构:

在这里插入图片描述

Mysql数据库JavaBean(Dao)
int idprivate Integer id
varchar(32) nameprivate String name
char(4) sexprivate String sex
Datetime borndateprivate Date borndate
varchar(32) phoneprivate String phone

对应dao层代码:

package com.ccl.dao;

import java.util.Date;

/**
 * @author: 13749
 * @date: 2021/9/23 17:18
 * @description:
 */
public class Actor {
    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(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Actor{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", borndate=" + borndate +
                ", phone='" + phone + '\'' +
                '}';
    }
}

在@Test模块中根据昨天的DruidUtil工具进行连接、获取相应表中数据,最后进行对ArrayList 的封装

 @Test
    public void testSelect02(){
        Connection connection = null;
        String sql = "select * from actor where id = ?";
        PreparedStatement preparedStatement = null;
        ResultSet set = null;
        ArrayList<Actor > list = new ArrayList<Actor>();
        try {
            connection = JdbcUtilsByDruid.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            
            preparedStatement.setInt(1,1);
            //执行sql
            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");
                //把得到的记录封装到Actor对象,放入到list集合
                //相当于把查询到的记录结果封装到Actor对象
                list.add(new Actor(id, name, sex, borndate, phone));
            }
            //输出list集合中的数据用foreach遍历结果集也可
            System.out.println("list" + list);


            

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtilsByDruid.close(set,preparedStatement,connection);
        }

    }
}

在这里插入图片描述

总结:封装成结果集之后即使连接关闭也不影响数据的使用

下一期:ApDBUtils工具的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曹老板么

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值