Tuesday 6 June 2017

ResultSet in JDBC with code example

What is ResultSet in JDBC?


Java ResultSet is an interface in jdbc which extends Wrapper and AutoCloseable interfaces.

ResultSet is used to retrieve SQL select query result.

A ResultSet object maintains a cursor pointing to its current row of data in the table. Initially, the cursor positioned before the first row. 

ResultSet is not updatable and by default, the object of Resultset can be moved only forward direction.

But we can move the object of ResultSet interface in both direction i.e forward and backward direction by using TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE in createStatement method and make the object updatable by using below statement.

Statement stm = con.createStatement(

ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);  


Method of ResultSet Interface in Java

There are some methds of java jdbc ResultSet interface which is mostly used in jdbc programs.


1) public boolean next()

It is used to move the cursor to the one row next from the current position.



2) public boolean previous()

It is used to move the cursor to the one row previous from the current position.


3) public boolean first()

It is used to move the cursor to the first row in result set object.


4) public boolean last()

It is used to move the cursor to the last row in result set object.


5) public boolean absolute(int row)

It is used to move the cursor to the specified row number in the result set object.Method of ResultSet Interface in Java
There are some methds of java jdbc ResultSet interface which is mostly used in jdbc programs.

6) public boolean relative(int row)

It is used to move the cursor to the relative row number in the result set object and it may be positive and negative.


7) public int getInt(int columnIndex)

It is used to return the data of specified column index of the current row as an int.


8) public int getInt(String columnName)

It is used to return the data of specified column name of the current row as an int.


9) public String getString(int columnIndex)

It is used to return the data of specified column index of the current row as String.


10) public String getString(String columnName)

It is used to return the data of specified column name of the current row as String.





Java ResultSet Example

Let's evaluate simple example of java jdbc ResultSet interface.

Suppose there is a student table in the mysql database with the rollno and name column. In this ResultSet example, We will fetch all the records from the student table by using select query.


import java.sql.*;
class ResultSetTest
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection(
jdbc:mysql://localhost/school?" +"user=java4732&password=test123");

Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select * from student");

while(rs.next());
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
}

catch(Exception e)
{
System.out.println(e);
}
}
}

That's all for Java ResultSet Example. Please write in comment section if you have any query on this topic. 

No comments:

Post a Comment