欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ResultSet结果集

程序员文章站 2022-03-06 18:58:46
...
1. ResultSet结果集的引入

当我们查询数据库时,返回的是一个二维的结果集,我们这时候需要使用ResultSet来遍历结果集,获取每一行的数据。


2. 使用ResultSet遍历查询结果

boolean next()将光标从当前位置向前移一行。
String getString(int columnIndex)以Java编程语言中String的形式获取此ResultSet对象的当前行中指定列的值。
String getString(String columnLabel)以Java编程语言中String的形式获取此ResultSet对象的当前行中指定列的值。


Book.java

package com.andrew.jdbc.model;

public class Book {
    private int id;
    private String bookName;
    private float price;
    private String author;
    private int bookTypeId;
    public Book(String bookName, float price, String author, int bookTypeId) {
        super();
        this.bookName = bookName;
        this.price = price;
        this.author = author;
        this.bookTypeId = bookTypeId;
    }
    public Book(int id, String bookName, float price, String author, int bookTypeId) {
        super();
        this.id = id;
        this.bookName = bookName;
        this.price = price;
        this.author = author;
        this.bookTypeId = bookTypeId;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public int getBookTypeId() {
        return bookTypeId;
    }
    public void setBookTypeId(int bookTypeId) {
        this.bookTypeId = bookTypeId;
    }
    @Override
    public String toString() {
        return "["+this.id+","+this.bookName+","+this.price+","+this.author+","+this.bookTypeId+"]";
    }
}

ResultSetTest.java

package com.andrew.jdbc.chap05;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.andrew.jdbc.model.Book;
import com.andrew.jdbc.util.DbUtil;

public class ResultSetTest {
    private static DbUtil dbUtil = new DbUtil();
    private static void listBook() throws Exception {
        Connection connection = dbUtil.getConnection();
        String sql = "select * from t_book";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String bookName = resultSet.getString(2);
            float price = resultSet.getFloat(3);
            String author = resultSet.getString(4);
            int bookTypeId = resultSet.getInt(5);
            System.out.println("图书编号:" + id + " 图书名称:" + bookName + " 图书价格:" 
                    + price + " 图书作者:" + author + " 图书类别id:" + bookTypeId);
            System.out.println("=========================================");
        }
    }
    private static void listBook2() throws Exception {
        Connection connection = dbUtil.getConnection(); // 获取连接
        String sql = "select * from t_book";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery(); // 返回结果集ResultSet
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String bookName = resultSet.getString("bookName");
            float price = resultSet.getFloat("price");
            String author = resultSet.getString("author");
            int bookTypeId = resultSet.getInt("bookTypeId"); // 获取第五列的值 图书类别id
            System.out.println("图书编号:" + id + " 图书名称:" + bookName + " 图书价格:" 
                    + price + " 图书作者:" + author + " 图书类别id:" + bookTypeId);
            System.out.println("=========================================");

        }
    }
    private static List<Book> listBook3() throws Exception {
        List<Book> bookList = new ArrayList<Book>();
        Connection connection = dbUtil.getConnection();
        String sql = "select * from t_book";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String bookName = resultSet.getString("bookName");
            float price = resultSet.getFloat("price");
            String author = resultSet.getString("author");
            int bookTypeId = resultSet.getInt("bookTypeId");
            Book book = new Book(id, bookName, price, author, bookTypeId);
            bookList.add(book);
        }
        return bookList;
    }
    public static void main(String[] args) throws Exception {
        System.out.println("-----------------------------------");
        listBook();
        System.out.println("-----------------------------------");
        listBook2();
        System.out.println("-----------------------------------");
        List<Book> bookList = listBook3();
        for (Book book : bookList) {
            System.out.println(book);
        }
    }
}

运行结果:
-----------------------------------
图书编号:1 图书名称:java学习 图书价格:666.0 图书作者:张三 图书类别id:1
=========================================
图书编号:2 图书名称:Java修改 图书价格:666.0 图书作者:修改人2 图书类别id:1
=========================================
-----------------------------------
图书编号:1 图书名称:java学习 图书价格:666.0 图书作者:张三 图书类别id:1
=========================================
图书编号:2 图书名称:Java修改 图书价格:666.0 图书作者:修改人2 图书类别id:1
=========================================
-----------------------------------
[1,java学习,666.0,张三,1]
[2,Java修改,666.0,修改人2,1]
相关标签: jdbc