Java ResultSet教程

Java ResultSet是JDBC框架的重要组成部分,用于访问从数据库查询得到的数据。它可以向前移动、滚动,并提供多种类型和并发性选择。ResultSet提供了导航、读取、更新和关闭等方法,允许程序员按需遍历和操作数据库记录。
摘要由CSDN通过智能技术生成

Java ResultSet interface is a part of the java.sql package. It is one of the core components of the JDBC Framework. ResultSet Object is used to access query results retrieved from the relational databases.

Java ResultSet接口是java.sql包的一部分。 它是JDBC框架的核心组件之一。 ResultSet对象用于访问从关系数据库检索的查询结果。

ResultSet maintains cursor/pointer which points to a single row of the query results. Using navigational and getter methods provided by ResultSet, we can iterate and access database records one by one. ResultSet can also be used to update data.

ResultSet维护指向查询结果的单行的光标/指针。 使用ResultSet提供的导航和获取方法,我们可以一个一个地迭代和访问数据库记录。 ResultSet也可以用于更新数据。

Java ResultSet层次结构 (Java ResultSet Hierarchy)

Java ResultSet Class Hierarchy
Java ResultSet Class Hierarchy
Java ResultSet类层次结构

The above diagram shows the place of ResultSet in the JDBC Framework. ResultSet can be obtained by executing SQL Query using Statement, PreparedStatement or CallableStatement.

上图显示了ResultSet在JDBC Framework中的位置。 通过使用StatementPreparedStatementCallableStatement执行SQL查询可以获得ResultSet

AutoCloseable, Wrapper are super interfaces of ResultSet.  Now we will see how to work with ResultSet in our Java programs.

AutoCloseableWrapper是ResultSet的超级接口。 现在,我们将看到如何在Java程序中使用ResultSet。

ResultSet示例 (ResultSet Example)

We will be using MySQL for our example purpose. Use below DB script to create a database and table along with some records.

我们将使用MySQL作为示例。 使用下面的DB脚本来创建数据库和表以及一些记录。


create database empdb;

use empdb;

create table tblemployee (empid integer primary key, firstname varchar(32), lastname varchar(32), dob date);

insert into tblemployee values  (1, 'Mike', 'Davis',' 1998-11-11');
insert into tblemployee values  (2, 'Josh', 'Martin', '1988-10-22');
insert into tblemployee values  (3, 'Ricky', 'Smith', '1999-05-11');

Let’s have look at the below example program to fetch the records from the table and print them on the console. Please make sure you have the MySQL JDBC driver in the project classpath.

让我们看下面的示例程序,从表中获取记录并将其打印在控制台上。 请确保在项目类路径中有MySQL JDBC驱动程序。


package com.journaldev.examples;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

/**
 * Java Resultset Example of Retrieving records.
 * 
 * @author pankaj
 *
 */

public class ResultSetDemo {

	public static void main(String[] args) {
		String query = "select empid, firstname, lastname, dob from tblemployee";
		Connection conn = null;
		Statement stmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/empdb", "root", "root");
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			while (rs.next()) {
				Integer empId = rs.getInt(1);
				String firstName = rs.getString(2);
				String lastName = rs.getString(3);
				Date dob = rs.getDate(4);
				System.out.println("empId:" + empId);
				System.out.println("firstName:" + firstName);
				System.out.println("lastName:" + lastName);
				System.out.println("dob:" + dob);
				System.out.println("");
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (Exception e) {}
		}
	}
}

Output:

输出:


empId:1
firstName:Mike
lastName:Davis
dob:1998-11-11

empId:2
firstName:Josh
lastName:Martin
dob:1988-10-22

empId:3
firstName:Ricky
lastName:Smith
dob:1999-05-11

Explanation:

说明

  • ResultSet is obtained by calling the executeQuery method on Statement instance. Initially, the cursor of ResultSet points to the position before the first ro
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值