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);
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.
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.
Let's simple example of java jdbc ResultSet interface.
Suppose there is a student table in the oracle database with the rollno and name column.
You can read this post : Oracle Database Connection in Java and Jdbc Statement interface.
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.
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.
Let's simple example of java jdbc ResultSet interface.
Suppose there is a student table in the oracle database with the rollno and name column.
You can read this post : Oracle Database Connection in Java and Jdbc Statement interface.
Java ResultSet Example
In this ResultSet example, We will fetch all the records from the student table by using select query.
import java.sql.*;
class ResultSetExample
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","bca");
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);
}
}
}
Read More:
How to Connect Java Program with MySql Database.
Steps to Connect to Database in Java.
Java Interface Concept with Examples.
Some Basic Java Programs for Beginners.
Star Pattern Programs in Java.
Alphabet Pattern Programs in Java.
The above java jdbc ResultSet example will fetch all the records from the student table.
import java.sql.*;
class ResultSetExample
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","bca");
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);
}
}
}
Read More:
How to Connect Java Program with MySql Database.
Steps to Connect to Database in Java.
Java Interface Concept with Examples.
Some Basic Java Programs for Beginners.
Star Pattern Programs in Java.
Alphabet Pattern Programs in Java.
The above java jdbc ResultSet example will fetch all the records from the student table.
Thank you for this jdbc and other java tutorials.
ReplyDeleteGreat Article android based projects
DeleteJava Training in Chennai
Project Center in Chennai
Java Training in Chennai
projects for cse
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Very Nice.. Really good for the Java developer. These question student in Java Training for getting placement.
ReplyDeleteHi, Very Nice Article.
ReplyDeleteThanks For The Sharing.
CIIT Noida provides Best MCA Courses in Noida based on the current
ReplyDeleteIT industry standards that help students to get high paying jobs in Top MNCs. CIIT provides Best MCA Training in Noida, Greater Noida, and Ghaziabad.
CIIT is one of the trusted MCA training institutes in Noida providing practical knowledge and 100% job assistance with basic as well as advanced
level MCA subjects. CIITN is the best MCA college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin.
At CIIT MCA classes in Noida is conducted by subject experts corporate professionals with 9+ years of experience in managing real-time and live
projects. Sofracle Nano Specialized MCA classes Noida is the perfect blend of academic learning and practical sessions to provide maximum exposure to
students that transform an average student into a corporate professional whom companies prefer to hire.
Best MCA College in Noida
Thanks for sharing this informative blog java training in chennai
ReplyDeleteI was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeleteArtificial Intelligence Training
Java Training
AWS Training
Machine Learning Training
Data Science Training
DevOps Training
Best article, very useful and explanation. Your post is extremely incredible. Thank you very much.
ReplyDeleteVisit us: Java Online Training Hyderabad
Visit us: Core Java Online Course
Hi, I read your whole blog. This is very nice. Good to know about the career in qa automation is broad in future. We are also providing various Java Training, anyone interested can Java Training Online for making their career in this field .
ReplyDeleteHi, I read your whole blog. This is very nice. Good to know about the career in qa automation is broad in future. We are also providing various Java Training, anyone interested can Java Training Online for making their career in this field .
ReplyDeleteAwesome article post.
ReplyDeleteCore java online training Hyderabad
java online training india
Amazing information you have shared with us. Everyone should read this. By Hire Cake PHP Developer
ReplyDelete