04ResultSet类详解

package cn.itcast.jdbc;

import java.sql.*;

/**

  • 执行DDL语句

*/
public class JDBCDemo6 {

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        //1. 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接对象
        conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
        //3.定义sql
        String sql  = "select * from account";
        //4.获取执行sql对象
        stmt = conn.createStatement();
        //5.执行sql
        rs = stmt.executeQuery(sql);
        //6.处理结果
        //6.1 让游标向下移动一行
        rs.next();
        //6.2 获取数据
        int id = rs.getInt(1);
        String name = rs.getString("name");
        double balance = rs.getDouble(3);

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


        //6.1 让游标向下移动一行
        rs.next();
        //6.2 获取数据
        int id2 = rs.getInt(1);
        String name2 = rs.getString("name");
        double balance2 = rs.getDouble(3);

        System.out.println(id2 + "---" + name2 + "---" + balance2);

        //6.1 让游标向下移动一行
        rs.next();
        //6.2 获取数据
        int id3 = rs.getInt(1);
        String name3 = rs.getString("name");
        double balance3 = rs.getDouble(3);

        System.out.println(id3 + "---" + name3 + "---" + balance3);


    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        //7.释放资源

        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

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

}

//getInt() getString() 数字:表示列的编号 文字:列的名称 get什么就是获取什么
不进行循环的话可能报错

clipboard.png
package cn.itcast.jdbc;

import java.sql.*;

/**

  • 执行DDL语句

*/
public class JDBCDemo7 {

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        //1. 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接对象
        conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
        //3.定义sql
        String sql  = "select * from account";
        //4.获取执行sql对象
        stmt = conn.createStatement();
        //5.执行sql
        rs = stmt.executeQuery(sql);
        //6.处理结果
        //循环判断游标是否是最后一行末尾。
        while(rs.next()){

            //获取数据
            //6.2 获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double balance = rs.getDouble(3);

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


       /* //6.1 让游标向下移动一行
        if(rs.next()){
            //判断是否有数据
            //6.2 获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double balance = rs.getDouble(3);

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

        //6.1 让游标向下移动一行
        if(rs.next()){
            //判断是否有数据
            //6.2 获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double balance = rs.getDouble(3);

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

        //6.1 让游标向下移动一行
        if(rs.next()){
            //判断是否有数据
            //6.2 获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double balance = rs.getDouble(3);

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

        //6.1 让游标向下移动一行
        if(rs.next()){
            //判断是否有数据
            //6.2 获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double balance = rs.getDouble(3);

            System.out.println(id + "---" + name + "---" + balance);
        }*/

      /*  //6.1 让游标向下移动一行
        rs.next();
        //6.2 获取数据
        int id2 = rs.getInt(1);
        String name2 = rs.getString("name");
        double balance2 = rs.getDouble(3);

        System.out.println(id2 + "---" + name2 + "---" + balance2);

        //6.1 让游标向下移动一行
        rs.next();
        //6.2 获取数据
        int id3 = rs.getInt(1);
        String name3 = rs.getString("name");
        double balance3 = rs.getDouble(3);

        System.out.println(id3 + "---" + name3 + "---" + balance3);*/


    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        //7.释放资源

        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

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

}
其中next()这个方法返回的是布尔值 当没有数据的时候返回false

评论

暂无评论