`
nugunaga
  • 浏览: 2966 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

20110211 ResultSet Learning

阅读更多
A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options.

       Statement stmt = con.createStatement(
                                      ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
       ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
       // rs will be scrollable, will not show changes made by others,
       // and will be updatable


The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.
For the getter methods, a JDBC driver attempts to convert the underlying data to the Java type specified in the getter method and returns a suitable Java value. The JDBC specification has a table showing the allowable mappings from SQL types to Java types that can be used by the ResultSet getter methods.


Column names used as input to getter methods are case insensitive. When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned. The column name option is designed to be used when column names are used in the SQL query that generated the result set. For columns that are NOT explicitly named in the query, it is best to use column numbers. If column names are used, the programmer should take care to guarantee that they uniquely refer to the intended columns, which can be assured with the SQL AS clause.
	/**
	 * getMetaData and fill List<Map<String, Object>>result with ResultSet Data
	 * @param rs
	 * @param pageLength
	 * @return
	 * @throws SQLException
	 */
	private List<Map<String, Object>> getDetails(ResultSet rs) throws HoffException {
		ResultSetMetaData rsmd = null;
		List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
		try {
			if(rs==null){
				return results;
			}
			rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			int n = 0;
			while (rs.next()) {
				n++;
				Map<String, Object> rowMap = new HashMap<String, Object>();
				for (int i = 1; i <= columnCount; i++) {
					String colName = rsmd.getColumnName(i);
					if (Util.valueOf(colName).trim().length() == 0) {
						colName = String.valueOf(i);
					}
					rowMap.put(colName, rs.getObject(i));
				}
				results.add(rowMap);
				if (n > FETCH_SIZE) {
					break;
				}
			}

			return results;
		} catch (SQLException sqe) {
			logger.error("SQLException while getting paginated details");
			throw new HoffException(ErrorLevel.ERROR, ErrorType.DATABASE_ERROR, sqe);
		}
	}

A set of updater methods were added to this interface in the JDBC 2.0 API (JavaTM 2 SDK, Standard Edition, version 1.2). The comments regarding parameters to the getter methods also apply to parameters to the updater methods.

The updater methods may be used in two ways:

to update a column value in the current row. In a scrollable ResultSet object, the cursor can be moved backwards and forwards, to an absolute position, or to a position relative to the current row. The following code fragment updates the NAME column in the fifth row of the ResultSet object rs and then uses the method updateRow to update the data source table from which rs was derived.
       rs.absolute(5); // moves the cursor to the fifth row of rs
       rs.updateString("NAME", "AINSWORTH"); // updates the 
          // NAME column of row 5 to be AINSWORTH
       rs.updateRow(); // updates the row in the data source


to insert column values into the insert row. An updatable ResultSet object has a special row associated with it that serves as a staging area for building a row to be inserted. The following code fragment moves the cursor to the insert row, builds a three-column row, and inserts it into rs and into the data source table using the method insertRow.

       rs.moveToInsertRow(); // moves cursor to the insert row
       rs.updateString(1, "AINSWORTH"); // updates the 
          // first column of the insert row to be AINSWORTH
       rs.updateInt(2,35); // updates the second column to be 35
       rs.updateBoolean(3, true); // updates the third column to true
       rs.insertRow();
       rs.moveToCurrentRow();


A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

The number, types and properties of a ResultSet object's columns are provided by the ResulSetMetaData object returned by the ResultSet.getMetaData method.

分享到:
评论

相关推荐

    ResultSet

    ResultSet是Java数据库连接(JDBC)中的核心接口,它用于存储和检索数据库查询结果。当你执行SQL查询并从数据库获取数据时,结果会被封装在ResultSet对象中。在本篇文章中,我们将深入探讨ResultSet的主要概念、操作...

    ResultSet 转为listmap

    ResultSet 转为 List ResultSet 转为 List&lt;Map&gt; 是一种常见的数据处理操作。在 Java 中,使用 JDBC 连接数据库时,通常会返回一个 ResultSet 对象,该对象包含了查询结果集的所有记录。为了方便数据处理和使用,...

    java数据库连接ResultSet

    Java 数据库连接 ResultSet Java 数据库连接中的 ResultSet 是一个非常重要的概念,它包含符合 SQL 语句中条件的所有行,并且提供了对这些行中数据的访问。ResultSet 通过一套 get 方法访问当前行中的不同列,例如 ...

    ResultSet对象获取数据的各种方法

    ### ResultSet对象获取数据的各种方法 在Java编程语言中,`ResultSet`对象是处理数据库查询结果的核心组件之一。它充当一个可滚动的、可更新的数据表,用于存储从数据库执行SQL语句后返回的结果集。本文将详细介绍...

    ResultSet转化为json,json转化为List

    在Java编程中,数据处理是核心任务之一,而ResultSet、JSON和List是处理数据时常见的三种数据结构。ResultSet是数据库查询结果的载体,JSON是一种轻量级的数据交换格式,而List是Java集合框架中的动态数组。本文将...

    支持ResultSet的JTable

    5. **将ResultSet转换为TableModel**:为了让`JTable`能显示`ResultSet`的数据,我们需要将`ResultSet`的内容转换为`TableModel`。这通常涉及到遍历`ResultSet`,获取列名和值,然后添加到`TableModel`中。 6. **...

    JAVA 版本ResultSet 转换为JAVABEAN的工具类

    在Java编程中,ResultSet是处理数据库查询结果的主要接口,它由Statement或PreparedStatement对象执行SQL查询后返回。而JavaBean是一种符合特定规范的Java类,通常用于封装数据,便于数据的传输和操作。当我们从...

    ResultSet的属性

    ResultSet是Java数据库连接(JDBC)中的一个重要接口,它用于存储和检索数据库查询结果。当执行SQL查询后,结果会被封装成ResultSet对象,允许我们逐行遍历并访问查询返回的数据。在处理ResultSet时,了解其属性和元...

    javaResultSet常用方法.pdf

    Java ResultSet常用方法 Java ResultSet是Java数据库连接(JDBC)中最重要的组件之一,用于存储和处理数据库查询结果。在Java中,ResultSet对象是通过Statement对象的executeQuery()方法或prepareStatement()方法...

    ResultSet用法集锦

    在Java编程语言中,`ResultSet`是用于存储和处理数据库查询结果的核心接口。它是由`Statement`或`PreparedStatement`执行SQL查询后返回的结果。本文将深入探讨`ResultSet`的使用方法,结合源码分析和实用工具,以...

    poi根据ResultSet到处Excle源码

    标题中的“poi根据ResultSet到处Excle源码”指的是使用Java的Apache POI库将数据库查询结果(ResultSet)转换为Excel文件的过程。Apache POI是一个流行的API,它允许开发者读写Microsoft Office格式的文件,包括...

    resultset2xml

    在IT行业中,数据库查询结果通常以ResultSet对象的形式返回,它是一种存储查询结果的接口,源自Java的JDBC(Java Database Connectivity)API。当需要将这些数据转换为XML格式时,以便于数据交换、存储或进一步处理...

    如何从 Java 存储过程将 JDBC ResultSet 作为 Ref Cursor 返回.doc

    JDBC提供了一种标准的方式来访问各种数据库,包括SQL查询的执行、结果集(ResultSet)的处理等。ResultSet是JDBC中用于存储查询结果的一个接口,它表示从数据库中检索的数据集。而REF CURSOR则是PL/SQL(Oracle...

    jsp 三种查询分页 resultset,hibernate ,存储过程

    jsp 三种查询分页 resultset,hibernate ,存储过程jsp 三种查询分页 resultset,hibernate ,存储过程jsp 三种查询分页 resultset,hibernate ,存储过程jsp 三种查询分页 resultset,hibernate ,存储过程

    ResultSet_to_json.jar

    标题“ResultSet_to_json.jar”指的是一个Java应用程序,其主要功能是将数据库查询结果集(ResultSet)以及List等数据结构转换为JSON(JavaScript Object Notation)格式的字符串。JSON是一种轻量级的数据交换格式,...

    ResultSet常用方法

    ResultSet是Java数据库连接(JDBC)中的一个核心接口,它用于存储从数据库查询返回的结果集。在处理SQL查询结果时,开发人员通常会与ResultSet对象交互以获取和操作数据。以下是对ResultSet常用方法的详细解释: 1....

    java resultset常用方法

    ### Java ResultSet 常用方法详解 #### 一、ResultSet 类型概述 在Java的JDBC编程中,`ResultSet`接口用于表示从数据库查询中获取的结果集。它提供了多种方式来处理这些数据,并且根据不同的应用场景,支持不同类型...

    获得结果集的字段名称_ResultSet的属性要调用ResultSetMetaData的方法

    获得结果集的字段名称_ResultSet的属性要调用ResultSetMetaData的方法 在Java中,获取结果集的字段名称可以通过调用ResultSetMetaData的方法来实现。ResultSetMetaData是一个公共接口,提供了关于ResultSet对象中列...

    java ResultSet 与 json互转所需要的全部包资源

    在java开发中常需要将ResultSet结果集转化为json格式以实现与客服端实现数据交互,但是这需要几个包,而且版本也要对应,因此我收集了全部的包,并测试可以运行,放在这里供大家下载。包括:commons-beanutils-1.7.0...

    ResultSet转List

    ResultSet转List

Global site tag (gtag.js) - Google Analytics