ResultSet In Java

This section will describe you how to store the database query result into resultset and fetched that data from resultset in Java.

ResultSet In Java

ResultSet In Java

In this section we will learn about the ResultSet in Java. We will read here about how to retrieve and modify the values from result sets. In this section we will also discuss about the various methods that are mostly used in to get the value.

ResultSet contains the data of a table after executing the SQL queries. Object of ResultSet also maintains the current position of the cursor to the current row. ResultSet provides next() method to move cursor in forward direction. next() method returns false when the cursor reached at the last row and the previous() method moves the cursor to the previous row. ResultSet object is automatically closed if the Statement/PreparedStatement object is committed to close, re-executed.

Commonly Used Methods Of ResultSet Object

Method Method Description
boolean absolute(int row) This method is used to change the location of the cursor at the specific row number.
Syntax : boolean absolute(int row) throws SQLException
void afterLast() This method is used to change the location of the cursor just after the last row to the end of the resultset.
Syntax : void afterLast() throws SQLException
void beforeFirst() This method is used to change the location of the cursor just before the first row to the front of the resultset.
Syntax : void beforeFirst() throws SQLException
void close() This method is used to close the result set. It releases the JDBC resources.
Syntax : void close() throws SQLException
void deleteRow() This method is used to delete the current row from both ResultSet object and the current database.
Syntax : void deleteRow() throws SQLException
boolean first() This method is used to change the location of the cursor to the first row.
Syntax : boolean first() throws SQLException
boolean last() This method is used to change the location of the cursor to the last row.
Syntax : boolean last() throws SQLException
Array getArray(int columnIndex) This method is used to get the Array object from the specified column index of the current row.
Syntax : Array getArray(int columnIndex) throws SQLException
InputStream getAsciiStream(int columnIndex) This method is used to get the stream of ASCII Characters from the specified column index of the current row.
Syntax : InputStream getAsciiStream(int columnIndex) throws SQLException
BigDecimal getBigDecimal(int columnIndex) This method is used to get the BigDecimal object from the specified column index of the current row.
Syntax : BigDecimal getBigDecimal(int columnIndex) throws SQLException
InputStream getBinaryStream(int columnIndex) This method is used to get the stream of uninterpreted bytes from the specified column index of the current row.
Syntax : InputStream getBinaryStream(int columnIndex) throws SQLException
Blob getBlob(int columnIndex) This method is used to get the Blob object from the specified column index of the current row.
Syntax : Blob getBlob(int columnIndex) throws SQLException
boolean getBoolean(int columnIndex) This method is used to get the boolean value from the specified column index of the current row.
Syntax : boolean getBoolean(int columnIndex) throws SQLException
byte getByte(int columnIndex) This method is used to get the byte value from the specified column index of the current row.
Syntax : byte getByte(int columnIndex) throws SQLException
byte[] getBytes(int columnIndex) This method is used to get the array of byte value from the specified column index of the current row.
Syntax : byte[] getBytes(int columnIndex) throws SQLException
Reader getCharacterStream(int columnIndex) This method is used to get the character stream value as java.io.Reader object from the specified column index of the current row.
Syntax : Reader getCharacterStream(int columnIndex) throws SQLException
Clob getClob(int columnIndex) This method is used to get the Clob object from the specified column index of the current row.
Syntax : Clob getClob(int columnIndex) throws SQLException
Date getDate(int columnIndex) This method is used to get the date value from the specified column index of the current row.
Syntax : Date getDate(int columnIndex) throws SQLException
Date getDate(int columnIndex, Calendar cal) This method is used to get the date value from the specified column index and the constructed date using java.util.Calendar object of the current row.
Syntax : Date getDate(int columnIndex, Calendar cal) throws SQLException
double getDouble(int columnIndex) This method is used to get the value as primitive double from the specified column index of the current row.
Syntax : double getDouble(int columnIndex) throws SQLException
float getFloat(int columnIndex) This method is used to get the value as primitive float from the specified column index of the current row.
Syntax : float getFloat(int columnIndex) throws SQLException
int getInt(int columnIndex) This method is used to get the value as primitive int from the specified column index of the current row.
Syntax : int getInt(int columnIndex) throws SQLException
long getLong(int columnIndex) This method is used to get the value as primitive long from the specified column index of the current row.
short getShort(int columnIndex) This method is used to get the value as short from the specified column index of the current row.
Syntax : short getShort(int columnIndex) throws SQLException
String getString(int columnIndex) This method is used to get the value as String form the specified column index of the current row.
Syntax : String getString(int columnIndex) throws SQLException
Time getTime(int columnIndex) This method is used to get the value as time (java.sql.Time ) from the specified column index of the current row.
Syntax : Time getTime(int columnIndex) throws SQLException
Timestamp getTimestamp(int columnIndex) This method is used to get the value as timestam(java.sql.Timestamp) from the specified column index of the current row.
Syntax : Timestamp getTimestamp(int columnIndex) throws SQLException
boolean next() This method is used to change the location of the cursor forward to one row from its current position.
Syntax : boolean next() throws SQLException
boolean previous() This method is used to change the location of the cursor to the previous row.
Syntax : boolean previous() throws SQLException

The above method summary explains few of the methods. Many of the methods of ResultSet object are overloaded such as, methods that are used to retrieve the value as primitive data type value are overloaded as datatype getXXXX(datatype columnLabel) { The parameter columnLabel specifies the name of the field of the database table} for example, int getInt( columnLabel)

Example

Here I am giving a simple example which will demonstrate you about how to use the ResultSet in Java. In this example you will see how I have created the ResultSet and keept the database data after querying the SQL. For this we have to create a database table using a DBMS (I will use MySQL) and then we will insert some data into it and then we will create a Java Class where we will display the data stores in the result set.

ResultSetExample.java


import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ResultSetExample {
	
	String driverName = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/record";
	String user = "root";
	String password = "root";
	
	Connection conn= null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	String sql = "SELECT * FROM attendance";
	
	public void getData()
	{		
		try{
			Class.forName(driverName);			
		}
		catch(ClassNotFoundException cnfe)
		{
			System.out.println(cnfe);
		}
		
		try
		{
			conn = DriverManager.getConnection(url, user, password);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			System.out.println("Date \t\t Id \t Name \t Status");
			while(rs.next())
			{
				String date = rs.getString(1);
				String id = rs.getString(2);
				String name = rs.getString(3);
				String status = rs.getString(4);
				System.out.println(date+"\t "+id+"\t"+name+"\t"+status);
				
			}
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle);
		}		
	}
	public static void main(String args[])
	{
		ResultSetExample rse = new ResultSetExample();
		rse.getData();
	}
}

Output

When you will compile and execute the ResutlSetExample.java then the output will be as follows :

Download Source Code