JDBC - Result Sets



The SQL articulations that read information from a database question, return the information in an outcome set. The SELECT explanation is the standard method to choose columns from a database and view them in an outcome set. The java.sql.ResultSet interface speaks to the outcome set of a database query.

A ResultSet object keeps up a cursor that focuses to the present line in the outcome set. The expression "result set" alludes to the line and section information contained in a ResultSet object.

The techniques for the ResultSet interface can be separated into three classifications −

  • Navigational methods: Used to move the cursor around.

  • Get methods: Used to see the information in the segments of the present column being pointed by the cursor.

  • Update methods: Used to refresh the information in the segments of the present column. The updates would then be able to be refreshed in the fundamental database as well.

The cursor is versatile dependent on the properties of the ResultSet. These properties are assigned when the relating Statement that produces the ResultSet is created.

JDBC gives the accompanying association strategies to make proclamations with wanted ResultSet −

  • createStatement(int RSType, int RSConcurrency);

  • prepareStatement(String SQL, int RSType, int RSConcurrency);

  • prepareCall(String sql, int RSType, int RSConcurrency);

The first contention demonstrates the sort of a ResultSet object and the second contention is one of two ResultSet constants for indicating whether an outcome set is perused just or updatable.

Type of ResultSet

The conceivable RSType are given underneath. On the off chance that you don't determine any ResultSet type, you will naturally get one that is TYPE_FORWARD_ONLY.

Type Description
ResultSet.TYPE_FORWARD_ONLY The cursor can just push ahead in the outcome set.
ResultSet.TYPE_SCROLL_INSENSITIVE The cursor can look forward and in reverse, and the outcome set isn't touchy to changes made by others to the database that happen after the outcome set was created.
ResultSet.TYPE_SCROLL_SENSITIVE. The cursor can look forward and in reverse, and the outcome set is touchy to changes made by others to the database that happen after the outcome set was created.

Concurrency of ResultSet

The conceivable RSConcurrency are given underneath. In the event that you don't determine any Concurrency type, you will naturally get one that is CONCUR_READ_ONLY.

Concurrency Description
ResultSet.CONCUR_READ_ONLY Creates a read-just outcome set. This is the default
ResultSet.CONCUR_UPDATABLE Creates an updateable outcome set.

All our precedents composed so far can be composed as pursues, which instates a Statement item to make a forward-just, read just ResultSet object −

try { 

Statement stmt = conn.createStatement( 

ResultSet.TYPE_FORWARD_ONLY, 

ResultSet.CONCUR_READ_ONLY); 

} 

catch(Exception ex) { 

.... 

} 

finally { 

.... 

}

Navigating a Result Set

There are a few techniques in the ResultSet interface that include moving the cursor, including −

S.N. Methods & Description
1 public void beforeFirst() tosses SQLException

Moves the cursor just before the first row.

2 public void afterLast() tosses SQLException

Moves the cursor soon after the last row.

3 public boolean first() tosses SQLException

Moves the cursor to the first row.

4 public void last() tosses SQLException

Moves the cursor to the last row.

5 public boolean absolute(int push) tosses SQLException

Moves the cursor to the predefined row.

6 public boolean relative(int push) tosses SQLException

Moves the cursor the given number of lines forward or in reverse, from where it is presently pointing.

7 public boolean past() tosses SQLException

Moves the cursor to the past line. This strategy returns false if the past column is off the result set.

8 public boolean next() tosses SQLException

Moves the cursor to the following line. This technique returns false if there are no more columns in the result set.

9 public int getRow() tosses SQLException

Returns the line number that the cursor is pointing to.

10 public void moveToInsertRow() tosses SQLException

Moves the cursor to an extraordinary column in the outcome set that can be utilized to embed another line into the database. The present cursor area is remembered.

11 public void moveToCurrentRow() tosses SQLException

Moves the cursor back to the present line if the cursor is at present at the supplement push; something else, this strategy does nothing

The ResultSet interface contains many techniques for getting the information of the current row.

There is a get technique for every one of the conceivable information types, and each get strategy has two variants −

  • One that takes in a section name.

  • One that takes in a section index.

For precedent, if the section you are keen on review contains an int, you have to utilize one of the getInt() techniques for ResultSet −

S.N. Methods & Description
1 public int getInt(String columnName) tosses SQLException

Returns the int in the present line in the section named columnName.

2 public int getInt(int columnIndex) tosses SQLException

Returns the int in the present line in the predetermined section record. The section file begins at 1, which means the primary segment of a line is 1, the second segment of a line is 2, thus on.

Similarly, there are get techniques in the ResultSet interface for every one of the eight Java crude sorts, just as basic kinds, for example, java.lang.String, java.lang.Object, and java.net.URL.

There are likewise techniques for getting SQL information types java.sql.Date, java.sql.Time, java.sql.TimeStamp, java.sql.Clob, and java.sql.Blob. Check the documentation for more data about utilizing these SQL information types.

For a superior comprehension, let us consider Viewing - Example Code.

Updating a Result Set

The ResultSet interface contains a gathering of refresh strategies for refreshing the information of an outcome set.

As with the get techniques, there are two refresh strategies for every datum type −

  • One that takes in a segment name.

  • One that takes in a segment index.

For model, to refresh a String segment of the present column of an outcome set, you would utilize one of the accompanying updateString() strategies −

S.N.Methods & Description
1public void updateString(int columnIndex, String s) tosses SQLException

Changes the String in the predetermined segment to the estimation of s.

2 public void updateString(String columnName, String s) tosses SQLException

Similar to the past technique, then again, actually the segment is indicated by its name rather than its index.

There are refresh strategies for the eight crude information types, just as String, Object, URL, and the SQL information types in the java.sql package.

Updating a line in the outcome set changes the sections of the present column in the ResultSet object, however not in the hidden database. To refresh your progressions to the line in the database, you have to conjure one of the accompanying methods.

S.N. Methods & Description
1 public void updateRow()

Updates the present line by refreshing the relating column in the database.

2 public void deleteRow()

Deletes the present line from the database

3 public void refreshRow()

Refreshes the information in the outcome set to mirror any ongoing changes in the database.

4 public void cancelRowUpdates()

Cancels any updates made on the current row.

5 public void insertRow()

Inserts a column into the database. This technique must be conjured when the cursor is indicating the supplement row.






© Javacodegeeks 2019 - 2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3