JDBC类详解_ResultSet_遍历结果集和JDBC练习_select语句

JDBC类详解_ResultSet_遍历结果集

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

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

注意:

  使用步骤:

    1.游标向下移动一行

    2.判断是否有数据

    3.获取数据

    private static void ResultSet1() {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///db2", "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表的数据将其封装为对象,然后装载集合,返回。

  1.定义Emp类

  2.定义方法 public List<Emp> findAll(){}

  3.实现方法 select * from emp;

    public static void main(String[] args) throws Exception {
        List<Emp> list = new JDBCD01().findAll();
        System.out.println(list);
        System.out.println(list.size());
    }
    
    /*
        定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
    */
    public List<Emp> findAll(){
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        List<Emp> list = null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
            //定义sql
            String sql = "select * from emp";
            //获取执行sql的对象 Statement
            stat = conn.createStatement();
            rs = stat.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 mgr = rs.getInt("mgr");
                Date joindate = 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.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBonus(bonus);
                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 (stat!=null){
                try {
                    stat.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

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

 

 

 

posted @ 2022-07-26 16:59  魔光领域  阅读(53)  评论(0编辑  收藏  举报