首页 > 其他分享 >550JDBC各个类详解_ResultSet_遍历结果集 and551 JDBC练习_select语句

550JDBC各个类详解_ResultSet_遍历结果集 and551 JDBC练习_select语句

时间:2022-10-17 10:01:46浏览次数:74  
标签:rs int ResultSet 550JDBC public and551 emp null id

JDResultSetBC各个类详解-ResultSet-遍历结果集

Boolean next() : 游标向下移动一行,判断当前行是否是最后一行末尾(是否有数据),

如果是,则返回false,如果不是则返回true

注意:

  使用步骤:

    1.游标向下移动一行

    2.判断是否有数据

    3.获取数据

public static void main(String[] args) throws Exception {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
            //定义sql
            String sql = "select * from account";
            //获取执行sql的对象 Statement
            stat = conn.createStatement();
            //执行sql
            rs = stat.executeQuery(sql);
            //处理结果

            //循环判断游标是否是最后一行末尾
            while (rs.next()) {
                //获取数据
                int id = rs.getInt(1);
                String name = rs.getString("name");
                double balance = rs.getDouble(3);

                System.out.println(id + "-" + name + "-" + balance);
            }

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

            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
  }   

 JDBC练习_select语句

/*
定义emp表
 */
public class emp {
    private int id;
    private String ename;
    private int job_id;
    private int mgt;
    private Date joindate;
    private double salary;
    private double bonus;
    private int 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 getMgt() {
        return mgt;
    }

    public void setMgt(int mgt) {
        this.mgt = mgt;
    }

    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 getBonus() {
        return bonus;
    }

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

    public int getDept_id() {
        return dept_id;
    }

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

    @Override
    public String toString() {
        return "emp{" +
                "id=" + id +
                ", ename='" + ename + '\'' +
                ", job_id=" + job_id +
                ", mgt=" + mgt +
                ", joindate=" + joindate +
                ", salary=" + salary +
                ", bouns=" + bonus +
                ", dept_id=" + dept_id +
                '}';
    }
}
/*
定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
 */
public class JDBC_demo01lx {
    public static void main(String[] args) {
        List<emp> list = new JDBC_demo01lx().findAll();
        System.out.println(list );
    }
    /*
    查询所有emp对象
     */
    ResultSet rs = null;
    Statement stmt = null;
    Connection conn=null;
    List<emp> list=null;
    public List<emp> findAll(){
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/practice?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root");
            //定义sql
            String sql = "select * from emp";
            //获取sql执行的对象
            stmt = conn.createStatement();
            //执行sql
            rs = stmt.executeQuery(sql);
            //遍历结果集,封装对象,装载集合
            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 mgt = rs.getInt("mgt");
                Date joindata = rs.getDate("joindate");
                double salary = rs.getDouble("salary");
                double bonus = rs.getDouble("bonus");
                int dept_id = rs.getInt("dept_id");

                //创建emp对象
                emp =new emp();
                emp.setId(id);
                emp.setEname(ename);
                emp.setJob_id(job_id);
                emp.setMgt(mgt);
                emp.setJoindate(joindata);
                emp.setSalary(salary);
                emp.setBonus(bonus);
                emp.setDept_id(dept_id);

                //装载集合
                list.add(emp);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (stmt!=null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    if (conn !=null){
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return list;
    }
}

 

标签:rs,int,ResultSet,550JDBC,public,and551,emp,null,id
From: https://www.cnblogs.com/agzq/p/16797153.html

相关文章