Reading:  

Getting started with JDBC


Working with Result Sets

The result set is the output of the database query data from the SQL statement. The SELECT statements read data from database query return data in to result set.

Object Result set is used to maintain cursor point of current row in result set. The result set is the combination of column and row data in object. The Result set interface has three categories in method:

  • Navigational methods: It is cursor to move around.
  • Method Get: display the data in columns of current row pointed by cursor.
  • Method Update: used to update data in columns of current row

JDBC allow following connection methods:

  • createStatement(int RS_Type, int RS_Concurrency);
  • prepareStatement(String sql_query, int RS_Type, int RS_Concurrency);
  • prepareCall(String sql_query, int RS_Type, int RS_Concurrency);

 

Type of Result Set: The type of resultset is shown below:

Type

Description

ResultSet.TYPE_SCROLL_INSENSITIVE

Cursor can scroll backwards and forwards and result set is not sensitive to changes done on database by others.

ResultSet.TYPE_SCROLL_SENSITIVE.

Cursor can scroll backwards and forwards and result set is sensitive to changes done on database by others.

ResultSet.TYPE_FORWARD_ONLY

It is forward only cursor.

 

Concurrency of Result Set: The types of concurrency are:

Concurrency

Description

ResultSet.CONCUR_READ_ONLY

Resultset is readonly.

ResultSet.CONCUR_UPDATABLE

Resultset is updatable.

 

try {
Statement stmtmnt = conn1.createStatement(
ResultSet.CONCUR_READ_ONLY,
ResultSet.TYPE_FORWARD_ONLY
);
}
catch(Exception ex) {
}
finally {
}
 

Navigate Result Set: Some of the methods on for moving the cursor are:

  • public void beforeFirst() throws SQLException{}

         Cursor is moved just before the First row

  • public boolean first() throws SQLException{}

   Cursor is moved to first row

  • public void afterLast() throws SQLException{}

    Cursor is moved after last row

  • public void last() throws SQLException{}

    Cursor is moved to last row

  • public boolean absolute(int row) throws SQLException{}

        Cursor is moved to specified row

  • public boolean previous() throws SQLException{}

        Cursor is moved to previous row.

  • public boolean next() throws SQLException{}

        Cursor is moved to next row.

  • public int getRow() throws SQLException{}

        Cursor pointing row number is returned.

Viewing a Result Set:

Getting the data from the resultset has two methods:

  • public int getInt(String columnName) throws SQLException{}

        Return current row position based on column name

  • public int getInt(int columnIndex) throws SQLException{}

        Return currentrow based on the column index

Updating a Result Set:

This will update data in result set. One takes column name and other takes the column index.

  • public void updateString(int columnIndexID, String s) throws SQLException{}

      Changes the string specified by the columnIndexID to s

  • public void updateString(String columnName, String s) throws SQLException{}

   Changes the string specified by the columnName to s

Update the resultset will not make any change in the database, it make changes to the resultset. If we have to make change to the database we need use below methods:

  • public void insertRow()

  It will insert new row into database

  • public void cancelRowUpdates()

  It will cancel the row update in database

  • public void deleteRow()

 It will delete row from the database

  • public void updateRow()

It will update row into the database.


 

Description

This tutorial is focused on getting you started with JDBC 4.1. This tutorial has following parts

  1. Introduction
  2. SQL Syntax
  3. Setting up Environment
  4. Getting started with some samples
  5. Connecting to database
  6. Statements
  7. Result Sets
  8. Data Types
  9. Transactions
  10. Exception handling
  11. Batch Processing
  12. Streaming Data
  13. More JDBC Examples

Leave your feedback to help us improve next time. Also let us know if you see any errors that needs to be corrected



Prerequisites

Understanding of Java language a must to understand various things in this tutorial

Audience

Absolute beginners who wants to get started with JDBC

Author: Subject Coach
Added on: 8th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!