Java -- JDBC学习笔记2、结果集

1、ResultSet(结果集)

  • 在执行查询SQL后,存放查询到的结果集数据。

1.1、接收结果集

String querySql = "SELECT Name,LoginName FROM Students";

        ResultSet rs = null;
        try
        {
            rs = statement.executeQuery(querySql);
        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();
        }

1.2、遍历ResultSet中的数据

ResultSet以表(table)结构进行临时数据的存储,需要通过JDBC AP将其中数据进行依次获取。

  1. 数据行指针:初始位置在第一行数据前,没调用一次boolean next()方法后,ResultSet的指针向下移动一行,结果为true,表示当前有数据。
  2. rs.getXxx(整数):代表根据列的编号顺序获得,从1开始,比如数据库表中有三个字段,第一个是String类的Name,那么getString(1)就会取出当前列列Name的值。
  3. rs.getXxx(“列名”):代表根据列名获得。
  4. Boolean next():判断rs结果集中下一行是否有数据。
1.2.1、遍历方法
try
        {
            while (rs.next())
            {
                //通过列名获取
                String name1 = rs.getString("Name");
                String loginName1 = rs.getString("LoginName");
                System.out.println("姓名:" + name1 + ",登录名:" + loginName1);

                //通过编号获取
                String name2 = rs.getString(1);
                String loginName2 = rs.getString(2);
                System.out.println("姓名:" + name1 + ",登录名:" + loginName1);
            }
        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();
        }

注意、不是只可以获取String类型,可以是各种类型的,要根据数据库中字段类型来获取,比如:

  • int getInt(int columnIndex) throws SQLexception
  • int getInt(String columnLabel) throws SQLexception
  • double getDouble(int columnIndex) throws SQLexception
  • double getDouble(String columnLabel) throws SQLexception

................

注意、列的编号从1开始。不是0,和数组不一样。

1.3、综合案例,查询Students表中的Name和LoginName

try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        Connection conn = null;

        String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentManage";

        try
        {
            conn = DriverManager.getConnection(url, "sa", "sql2012");
        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();
        }
        Statement statement = null;
        try
        {
            statement = conn.createStatement();
        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();
        }
        String querySql = "SELECT Name,LoginName FROM Students";

        ResultSet rs = null;
        try
        {
            rs = statement.executeQuery(querySql);
        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();
        }

        try
        {
            while (rs.next())
            {
                //通过列名获取
                String name1 = rs.getString("Name");
                String loginName1 = rs.getString("LoginName");
                System.out.println("姓名:" + name1 + ",登录名:" + loginName1);

                //通过编号获取
                String name2 = rs.getString(1);
                String loginName2 = rs.getString(2);
                System.out.println("姓名:" + name1 + ",登录名:" + loginName1);
            }
        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();
        }
        try
        {
            rs.close();
            statement.close();
            conn.close();
        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();
        }
原文地址:https://www.cnblogs.com/dcy521/p/14725476.html