米字键

博客园 首页 新随笔 联系 订阅 管理

创建员工类  自动生成get set 方法

package cn.hph;

public class emp {
    //定义表中的属性
   private  int id;
   private String ename;
   private int Job_id;
   private int mgr;
   private String  Joindate;
   private Double salary;
   private Double bonus;
   private int dept_id;
   //生成get set方法

    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) {
        Job_id = job_id;
    }

    public int getMgr() {
        return mgr;
    }



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

    public String getJoindate() {
        return Joindate;
    }

    public void setJoindate(String joindate) {
        Joindate = joindate;
    }

    public Double getSalary() {
        return salary;
    }

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

    public Double getBonus() {
        return bonus;
    }

    public void setBonus(Double bonus) {
        this.bonus = bonus;
    }

    public int getDept_id() {
        return dept_id;
    }

    public void setDept_id(int dep_id) {
        this.dept_id = dep_id;
    }
    
    //生成tostring方法
    public String toString() {
        return "id=" + id +
                ", ename='" + ename + '\'' +
                ", Job_id=" + Job_id +
                ", mgr=" + mgr +
                ", Joindate='" + Joindate + '\'' +
                ", salary=" + salary +
                ", bonus=" + bonus +
                ", dep_id=" + dept_id;
    }
}

查询员工表 代码如下:

package cn.hph;

import jdbcUtil.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdpcTest {
    public static void main(String[] args) throws SQLException {
        try {
            //new 员工类
            emp em=new emp();
            //调用jdbc工具类
            Connection conn= JdbcUtil.getConnection();
            //获取PreparedStatement对象 sql语句
            PreparedStatement pre = conn.prepareStatement("select *from emp");
            //执行sql语句
            ResultSet rs = pre.executeQuery();
            //使用while循环获取员工表中的列
            while(rs.next()){
                em.setId(rs.getInt("id"));
                em.setEname(rs.getString("ename"));
                em.setJob_id(rs.getInt("job_id"));
                em.setMgr(rs.getInt("mgr"));
                em.setJoindate(rs.getString("joindate"));
                em.setSalary(rs.getDouble("salary"));
                em.setBonus(rs.getDouble("bonus"));
                em.setDept_id(rs.getInt("dept_id"));
                String s = em.toString();
                System.out.println(s);
            }
            //关闭jdbc
            JdbcUtil.close(rs,pre,conn);

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

    }


    }

 

posted on 2019-04-02 21:56  米字键  阅读(641)  评论(0编辑  收藏  举报