遍历JDBC结果集

遍历结果集:

案例5:JDBC如何获取查询返回结果集:

package com.java.JDBC;


import java.sql.*;


public class JDBCTest05 {
    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://localhost:3306/mydatabase","root","333");
            // 3 获取数据库操作对象
            stmt = conn.createStatement();
            // 4 执行sql语句
            String sql = "select empno as a,ename,sal from emp";
            // int executeQuery(select);
            // ResultSet executeUpdate(insert/update/delete);
            rs = stmt.executeQuery(sql);// 专门执行DQL语句的方法
            // 5 处理查询结果集
            /*boolean flag = rs.next();
            // System.out.println(flag);// true
            
            if(flag){
                // 光标指向的行有数据
                // 取数据
                // getString()方法的特点是:不管数据库中的数据类型是什么,都以String的形式取出。
                // 以下程序中的 1 2 3 说的是第几列
                String empno = rs.getString(1);// JDBC中所有下标从1开始。不是从0开始
                String ename = rs.getString(2);
                String sal = rs.getString(3);
                System.out.println(empno + " " + ename + " " + sal);
            }*/
            
            while (rs.next()){
                /*String empno = rs.getString(1);
                String ename = rs.getString(2);
                String sal = rs.getString(3);*/
                
                /*// 这个不是以列的下标获取
                String empno = rs.getString("a");// 重点注意:列名称不是表中的列名称,是查询结果的列名称
                String ename = rs.getString("ename");
                String sal = rs.getString("sal");
                System.out.println(empno + " " + ename + " " + sal);*/
    
                int empno = rs.getInt("a");// 重点注意:列名称不是表中的列名称,是查询结果的列名称
                String ename = rs.getString("ename");
                double sal = rs.getDouble("sal");
                System.out.println(empno + " " + ename + " " + sal + 100 );
                
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            // 6 释放资源
            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();
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/xlwu/p/13654053.html