zoukankan      html  css  js  c++  java
  • JDBC | 第五章: JDBC之ResultSet结果集遍历和数据的获取

    JDBC结果集

    1. SQL语句执行后从数据库查询读取数据,返回的数据放在结果集中 ResultSet接口表示数据库查询的结果集。
    2. ResultSet对象维护指向结果集中当前行的游标。 术语“结果集”是指包含在ResultSet对象中的行和列数据。

    浏览结果集

    编号 方法 描述
    1 public void beforeFirst() throws SQLException 将光标移动到第一行之前
    2 public void afterLast() throws SQLException 将光标移动到最后一行之后。
    3 public boolean first() throws SQLException 将光标移动到第一行。
    4 public void last() throws SQLException 将光标移动到最后一行。
    5 public boolean absolute(int row) throws SQLException 将光标移动到指定的行。
    6 public boolean relative(int row) throws SQLException 从当前指向的位置,将光标向前或向后移动给定行数。
    7 public boolean previous() throws SQLException 将光标移动到上一行。 如果上一行关闭结果集,此方法返回false。
    8 public boolean next() throws SQLException 将光标移动到下一行。 如果结果集中没有更多行,则此方法返回false。
    9 public int getRow() throws SQLException 返回光标指向的行号。
    10 public void moveToInsertRow() throws SQLException 将光标移动到结果集中的特殊行,该行可用于将新行插入数据库。当前光标位置被记住。
    11 public void moveToCurrentRow() throws SQLException 如果光标当前位于插入行,则将光标移回当前行; 否则,此方法什么也不做

    查看结果集

    编号 方法 描述
    序号 方法 描述
    1 public int getInt(String columnName) throws SQLException 返回名为columnName的列中当前行中的int值。
    2 public int getInt(int columnIndex) throws SQLException 返回指定列索引当前行中的int值。列索引从1开始,意味着行的第一列为1,行的第二列为2,依此类推。

    类似地,在八个Java基元类型中的每一个的ResultSet接口中都有get方法,以及常见的类型,如java.lang.String,java.lang.Object和java.net.URL等。

    简单使用

        /*
         * 简单数据获取遍历
         * */
        public void selectEasy() {
            Connection connection = null;
            Statement statement = null;
            ResultSet rs = null;
            String sql = "select * from user limit 0,50";
            try {
                //获取数据连接
                connection = basicUse.getConnection();
                //获取发送sql指令执行sql对象
                statement = connection.createStatement();
                //返回查询结果集用于保存数据库查询内容
                rs = statement.executeQuery(sql);
                //遍历结果集拿到数据
                //next()---------------类似指针的效果,会向下移动;
                while (rs.next()) {
                    // getString(String columnname)-------------根据列名获取本列的值;
                    //getString(int index)--------------根据索引获取指定位置的值;
                    System.out.println("id" + "\t" + rs.getString(1));
                    System.out.println("name" + "\t" + rs.getString(2));
                    System.out.println("age" + "\t" + rs.getString("age"));
                    System.out.println("email" + "\t" + rs.getString("email"));
                    System.out.println("manager_id" + "\t" + rs.getString("manager_id"));
                    System.out.println("create_time" + "\t" + rs.getString("create_time"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //执行完数据库操作后记得关闭数据库连接资源
                try {
                    rs.close();
                    statement.close();
                    connection.close();
    
                } catch (SQLException e) {
                    e.printStackTrace();
                }
    
            }
        }
    

    完整项目案例
    点击这里 github

    参考 https://www.jianshu.com/p/4557883d8f71

  • 相关阅读:
    POJ 1300 Open Door
    POJ 2230 Watchcow
    codevs 1028 花店橱窗布置
    codevs 1021 玛丽卡
    codevs 1519 过路费
    codevs 3287 货车运输
    codevs 3305 水果姐逛水果街二
    codevs 1036 商务旅行
    codevs 4605 LCA
    POJ 1330 Nearest Common Ancestors
  • 原文地址:https://www.cnblogs.com/kenx/p/13553931.html
Copyright © 2011-2022 走看看