JDBC(三)

练习:定义一个方法,将数据库的Emp表封装为对象,然后装载集合,返回

  1. 定义一个Emp类
  2. 定义方法,public List findAll(){}
  3. 实现方法 select * from emp;
    实现代码:
    创建Emp表的类
package com.yunji.JDBC;

import java.util.Date;

/**
 * 封装Emp表数据的javaBean
 */
public class Emp {
    private int id;
    private String ename;
    private int job_id;
    private int mgr;
    private Date joindate;
    private double salary;
    private double bounds;
    private int dept_id;


    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", ename='" + ename + '\'' +
                ", job_id=" + job_id +
                ", mgr=" + mgr +
                ", joindate=" + joindate +
                ", salary=" + salary +
                ", bounds=" + bounds +
                ", dept_id=" + dept_id +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public int getJob_id() {
        return job_id;
    }

    public void setJob_id(int job_id) {
        this.job_id = job_id;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public Date getJoindate() {
        return joindate;
    }

    public void setJoindate(Date joindate) {
        this.joindate = joindate;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getBounds() {
        return bounds;
    }

    public void setBounds(double bounds) {
        this.bounds = bounds;
    }

    public int getDept_id() {
        return dept_id;
    }

    public void setDept_id(int dept_id) {
        this.dept_id = dept_id;
    }
}

定义一个方法,将数据库的Emp表封装为对象,然后装载集合,返回

package com.yunji.JDBC;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 定义一个方法,将数据库的Emp表封装为对象,然后装载集合,返回
 */
public class JDBCDemo02 {
    public static void main(String[] args) {
        List<Emp> list = new JDBCDemo02().findAll();
        System.out.println(list);
    }
    public List<Emp> findAll(){
        Statement stmt =null;
        Connection conn =null;
        ResultSet rs = null;
        List<Emp> list = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc,Driver");
            //2.获取连接
             conn = DriverManager.getConnection("127.0.0.1", "root", "root");
            //3.定义sql
            String sql = "select * from Emp";
            //4.获取执行sql对象
             stmt = conn.createStatement();
            //5.通过stmt执行sql
             rs = stmt.executeQuery(sql);
            //6.遍历结果集,封装对象,装载集合
            Emp emp = null;
            list = new ArrayList<>();
            while (rs.next()){
                //获取数据 通过列名获取对应的数据
                int id = rs.getInt("id");
                String ename = rs.getString("ename");
                int job_id = rs.getInt("job_id");
                int mgr = rs.getInt("mgr");
                Date joindate = rs.getDate("joindate");
                double salary = rs.getDouble("salary");
                double bounds = rs.getDouble("bounds");
                int dept_id = rs.getInt("dept_id");
                /**
                 * 封装对象,因为之后对象有很多,我们如果直接用Emp emp = new emp(),然后再用emp.的方法
                 * 再去赋值的话,在栈内存中会产生很多的引用.如果我们创建一个Emp的引用,让这个引用复用.每次
                 * 进来的时候让emp重新去创建对象,达到复用的目的
                 */
                // 创建emp对象,并赋值
                emp= new Emp();
                emp.setId(id);
                emp.setEname(ename);
                emp.setJob_id(job_id);
                emp.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBounds(bounds);
                emp.setDept_id(dept_id);
                // 装载集合
                list.add(emp);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }


        }
        return list;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值