- Java & MySQL - Discussion
- Java & MySQL - Useful Resources
- Java & MySQL - Quick Guide
- Java & MySQL - Sorting Data
- Java & MySQL - Like Clause
- Java & MySQL - Where Clause
- Java & MySQL - Delete Records
- Java & MySQL - Update Records
- Java & MySQL - Select Records
- Java & MySQL - Insert Records
- Java & MySQL - Drop Tables
- Java & MySQL - Create Tables
- Java & MySQL - Drop Database
- Java & MySQL - Select Database
- Java & MySQL - Create Database
- Java & MySQL - Streaming Data
- Batch Processing - PreparedStatement
- Batch Processing - Statement
- Java & MySQL - Batch Processing
- Java & MySQL - SavePoint Transactions
- Java & MySQL - Commit & Rollback
- Java & MySQL - Transactions
- Java & MySQL - Update Result Set
- Java & MySQL - View Result Set
- Java & MySQL - Navigate Result Set
- Java & MySQL - Result Set
- Java & MySQL - CallableStatement
- Java & MySQL - PreparedStatement
- Java & MySQL - Statement
- Java & MySQL - Exceptions
- Java & MySQL - Connections
- Java & MySQL - Sample Code
- Java & MySQL - Environment Setup
- Java & MySQL - Overview
- Java & MySQL - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java & MySQL - ResultSet
The SQL statements that read data from a database query, return the data in a result set. The SELECT statement is the standard way to select rows from a database and view them in a result set. The java.sql.ResultSet interface represents the result set of a database query.
A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object.
The methods of the ResultSet interface can be broken down into three categories −
Navigational methods − Used to move the cursor around.
Get methods − Used to view the data in the columns of the current row being pointed by the cursor.
Update methods − Used to update the data in the columns of the current row. The updates can then be updated in the underlying database as well.
The cursor is movable based on the properties of the ResultSet. These properties are designated when the corresponding Statement that generates the ResultSet is created.
JDBC provides the following connection methods to create statements with desired ResultSet −
createStatement(int RSType, int RSConcurrency);
prepareStatement(String SQL, int RSType, int RSConcurrency);
prepareCall(String sql, int RSType, int RSConcurrency);
The first argument indicates the type of a ResultSet object and the second argument is one of two ResultSet constants for specifying whether a result set is read-only or updatable.
Type of ResultSet
The possible RSType are given below. If you do not specify any ResultSet type, you will automatically get one that is TYPE_FORWARD_ONLY.
Type | Description |
---|---|
ResultSet.TYPE_FORWARD_ONLY | The cursor can only move forward in the result set. |
ResultSet.TYPE_SCROLL_INSENSITIVE | The cursor can scroll forward and backward, and the result set is not sensitive to changes made by others to the database that occur after the result set was created. |
ResultSet.TYPE_SCROLL_SENSITIVE. | The cursor can scroll forward and backward, and the result set is sensitive to changes made by others to the database that occur after the result set was created. |
Concurrency of ResultSet
The possible RSConcurrency are given below. If you do not specify any Concurrency type, you will automatically get one that is CONCUR_READ_ONLY.
Concurrency | Description |
---|---|
ResultSet.CONCUR_READ_ONLY | Creates a read-only result set. This is the default |
ResultSet.CONCUR_UPDATABLE | Creates an updateable result set. |
All our examples written so far can be written as follows, which initiapzes a Statement object to create a forward-only, read only ResultSet object −
try( Statement stmt = conn.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);) } catch(Exception ex) { .... } finally { .... }Advertisements