JDBC各个详解-ResultSet基本使用以及遍历结果集

JDBC各个详解-ResultSet基本使用

ResultSet:结果集对象,封装查询结果

  next():游标向下移动一行

  getxxx():获取数据

    xxx:代表数据类型 如:int getInt()  String getString()

    参数:

      1.int:代表列的编号,从1开始 如:getInt(1)

      2.String:列的名称,如:getString("name")

代码:

复制代码
    public static void main(String[] args) {
        Connection conn = null;
        Statement state = null;
        ResultSet set = null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
            //定义sql
            String sql = "select * from account";
            //获取执行sql对象
            state = conn.createStatement();
            //执行sql
            set = state.executeQuery(sql);
            //处理数据
            //让游标向下移动一行 
            set.next();
            int id = set.getInt(1);
            String name = set.getString("name");
            double banlance = set.getDouble(3);

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

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if (set != null){
                try {
                    set.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (state != null){
                try {
                    state.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
复制代码

运行结果:

 

 

注意:

  使用步骤:

    1.游标向下移动一行

    2.判断是否有数据

    3.获取数据

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

复制代码
    public static void main(String[] args) {
        Connection conn = null;
        Statement state = null;
        ResultSet set = null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
            //定义sql
            String sql = "select * from account";
            //获取执行sql对象
            state = conn.createStatement();
            //执行sql
            set = state.executeQuery(sql);
            //处理数据
            //循环判断游标是否是最后一行末尾数据
            while (set.next()){//next():下一行
                int id = set.getInt(1);
                String name = set.getString("name");
                double banlance = set.getDouble(3);
                System.out.println(id + "-----" + name + "-----" + banlance);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if (set != null){
                try {
                    set.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (state != null){
                try {
                    state.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
复制代码

运行结果:

 

posted @   monkey大佬  阅读(489)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示