|NO.Z.00081|——————————|BigDataEnd|——|Java&MySQL.JDBC.V06|——|MySQL.v06|Jdbc开发_处理结果集对象|

一、API 使用: 处理结果集
### --- 只有在进行查询操作的时候, 才会处理结果集

——>        代码示例
public class JDBCDemo04 {
    
    public static void main(String[] args) throws SQLException {

       //1.注册驱动 可以省略

       //2.获取连接
       String url = "jdbc:mysql://localhost:3306/db4";
       Connection con = DriverManager.getConnection(url, "root", "123456");

       //3.获取 Statement对象
       Statement statement = con.createStatement();
        
       String sql = "select * from jdbc_user";
       //执行查询操作,返回的是一个 ResultSet 结果对象
       ResultSet resultSet = statement.executeQuery(sql);

        //4.处理结果集 resultSet
    }
}
二、ResultSet接口
### --- ResultSet接口

~~~     作用:封装数据库查询的结果集,对结果集进行遍历,取出每一条记录。
ResultSet接口方法 说明
boolean next()
1) 游标向下一行
2) 返回 boolean 类型,如果还有下一条记录,返回 true,否则返回 false
xxx getXxx( String or int)
1) 通过列名,参数是 String 类型。返回不同的类型
2) 通过列号,参数是整数,从 1 开始。返回不同的类型
三、释放资源
四、代码示例
public class JDBCDemo04 {

    public static void main(String[] args) throws SQLException {

        //1.注册驱动 可以省略

        //2.获取连接
        String url = "jdbc:mysql://localhost:3306/db4";
        Connection con = DriverManager.getConnection(url, "root", "123456");

        //3.获取 Statement对象
        Statement statement = con.createStatement();

        String sql = "select * from jdbc_user";
        //执行查询操作,返回的是一个 ResultSet 结果对象
        ResultSet resultSet = statement.executeQuery(sql);

        //4.处理结果集
//      //next 方法判断是否还有下一条数据
//      boolean next = resultSet.next();
//      System.out.println(next);
//
//      //getXXX 方法获取数据 两种方式
//      int id = resultSet.getInt("id");//列名
//      System.out.println(id);
//
//      int anInt = resultSet.getInt(1);//列号
//      System.out.println(anInt);
        //使用while循环
        while(resultSet.next()){
            //获取id
            int id = resultSet.getInt("id");
            //获取姓名
            String username = resultSet.getString("username");
            //获取生日
            Date birthday = resultSet.getDate("birthday");
            
            System.out.println(id + " = " +username + " : " + birthday);
        }

        //关闭连接
        resultSet.close();
        statement.close();
        con.close();
    }
}
五、sql语句
package com.yanqi.jdbc05;

import java.sql.*;

public class JdbcDemo02 {



    public static class JDBCDemo02 {

        public static void main(String[] args) throws Exception {

            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");

            //2.获取连接
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "123456");

            //3.获取语句执行平台对象
            Statement statement = con.createStatement();

            //4.执行查询操作 使用executeQuery()
            String sql = "SELECT * FROM jdbc_user;";

            //resultSet 是结果集对象
            ResultSet resultSet = statement.executeQuery(sql);

            //通过while循环 遍历获取 resultSet中的数据
            while(resultSet.next()){
                //获取id
                int id = resultSet.getInt("id");

                //获取姓名
                String username = resultSet.getString("username");

                //获取密码
                String password = resultSet.getString("password");

                //获取生日
                Date birthday = resultSet.getDate("birthday");

                System.out.println(id+" : " + username+" : " + password + " : " + birthday);
            }

            //5.关闭流操作
            resultSet.close();
            statement.close();
            con.close();
        }

    }

//处理结果集对象 resultSet
//        boolean next = resultSet.next();  //判断是否有下一条数据
//        System.out.println(next);

//获取id
//        int id = resultSet.getInt("id");
//        System.out.println("通过列名的方式获取 "+ id);

//        int id = resultSet.getInt(1);
//        System.out.println("通过列号获取id " + id);
}

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(28)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示