ResultSet结果集
程序员文章站
2022-03-06 18:58:46
...
1. ResultSet结果集的引入
2. 使用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]
上一篇: 霸屏新阵地,1分钟百度收录并排首页技术
下一篇: 使用元数据分析数据库
推荐阅读
-
排序算法大数据量测试结果
-
触发器满足条件执行后如何通知应用程序展现结果
-
hdu 3038How Many Answers Are Wrong 这辈子都学不会的带权并查集(略懂篇章)
-
hadoop-1.0.3集群安装
-
更改Oracle字符集:把字符集ZHS16GBK换成UTF8
-
mysql数据校验之字符集问题
-
求一下总记录数,好分页,但是count(*) 要2秒,查询时间记录结果只要0.0044秒解决思路
-
php怎么把mysql 的查询结果形成数字,有啥方便的办法
-
5.1tensorflow5.1神经网络调参实现mnist数据集分类正确率98%以上(实现动态学习率调整)
-
sphinx搜索结果同一个用户发布的信息太多如何打散?