封装工具类的练习

目录

 控制层DeptC

 执行层DeptDaoImpl

package com.hp.dao.impl;

import com.hp.dao.IDeptDao;
import com.hp.pojo.Dept;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
//impl表示实现类

public class DeptDaoImpl  implements IDeptDao {
    @Override
    public List<Dept> list() {
        //创建一个集合,存放所有的对象
        List<Dept>deptList=new ArrayList<>();
        try{
//1.注册驱动-反击去加载jar包中的com.mysql.jdbc.Driver这个类中的DriverManager.registerDriver(new Driver());
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接对象
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/summer-camp2023?characterEncoding=utf8","root","root");
            System.out.println(con);
            //3.定义sql
            String sql="select *from t_dept ";
            //4.需要创建statement
            Statement statement=con.createStatement();
            //5.statement执行sql,返回 结果集
            ResultSet rs=statement.executeQuery(sql);
            //6.解析rs
            while (rs.next()){//读取结果集的光标向下移动一行,光标默认在哪一行,列名所在的那一行
                int did = rs.getInt("did");
                String dname=rs.getString("dname");
                String dlocation=rs.getString("dlocation");
                String leader=rs.getString("leader");
                //根据每一行数据,封装成一个实体对象
                Dept dept=new Dept();
                dept.setDid(did);
                dept.getDname(dname);
                dept.getDlocation(dlocation);
                dept.getLeader(leader);
                deptList.add(dept);
            }

            //7,关闭资源
            statement.close();
            con.close();
        }catch ( Exception e){
            e.printStackTrace();
        }
        return  deptList;
    }
}

 服务层DeptServiceImpl

 执行层接口IDeptDao

 

服务层接口IDeptService

pojo包Dept类 

package com.hp.pojo;

public class Dept {
    private int did;
    private String dname;
    private String dlocation;
    private String leader;
//快捷键Alt+insert

    public static void main(String[] args) {
        System.out.println(new Dept());
    }

    @Override
    public String toString() {
        return "Dept{" +
                "did=" + did +
                ", dname=" + dname +
                ", dlocation='" + dlocation + '\'' +
                ", leader='" + leader + '\'' +
                '}';
    }

    public Dept(int did, String dname, String dlocation, String leader) {
        this.did = did;
        this.dname = dname;
        this.dlocation = dlocation;
        this.leader = leader;
    }
    public Dept(){

    }
    //封装

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getDname(String dname) {
        return this.dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getDlocation(String dlocation) {
        return this.dlocation;
    }

    public void setDlocation(String dlocation) {
        this.dlocation = dlocation;
    }

    public String getLeader(String leader) {
        return leader;
    }

    public void setLeader(String leader) {
        this.leader = leader;
    }
}

 

 结果:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值