Updated March 29, 2023
Introduction to JDBC resultset
The following article provides an outline for the JDBC resultset. Java provides the different types of interface to the user, in which that resultset is one, the interface that Java provides. Basically, we know that when we need to retrieve the data from a database, we can use a select statement, but Java provides some integrated function that is resultset. In other words, we can say that when we execute the SQL statement to read data from a database, it returns the data in the resultset. Normally select is the standard clause to select the rows from the database, and we can view in a resultset as per user requirement.
Syntax
Statement r_stmt_obj = con_obj.createStatement(ResultSet.specified type of resultset, ResultSet.concurrency type of resultset);
ResultSet rset_obj = r_stmt_obj.excuteQuery(required query statement);
Explanation
In the above syntax, we use the resultset interface to fetch the data from the Here,se, here we need to specify which type of resultset we need and well as which type of concurrency type we need to use as shown in the above syntax.
How does the resultset work in JDBC?
Now let’s see how the resultset works in JDBC as follows.
ResultSet Interface is available in the java.sql bundle. It is utilized to store the information which is retrieved from the data set table after the execution of the SQL articulations in the Java Program. The object of ResultSet keeps up with the cursor point at the outcome information. In default, the cursor positions before the principal column of the outcome information.
The next () strategy is utilized to move the cursor to the following situation in a forward way. It will return FALSE in case there are no more records. It recovers information by calling the executeQuery() technique utilizing any of the assertion objects. It very well might be a Statement or PreparedStatement or CallableStatement object.
Now let’s see the different types of ResultSet as follows.
Basically, ResultSet is used to return the data from the database. Therefore, we can emphasize the qualities in different ways utilizing Scrollable ResultSet.
TYPE_FORWARD_ONLY: it is the first type of resultset, and it is a default option that means in this type, movement of the cursor starts to end, or in other words, we can say that forward direction.
TYPE_SCROLL_INSENSITIVE: This is the second type of resultset, and in this type, the cursor’s movement in both directions is forward and backward directions. On the off chance that we roll out any improvements in the information while emphasizing the put-away information, it will not refresh in the dataset if anybody changes the information in DB. Since the dataset has the information from the time, the SQL question returns the Data.
TYPE_SCROLL_SENSITIVE: It is like TYPE_SCROLL_INSENSITIVE; the thing that matters is in the event that anybody refreshes the information after the SQL Query has returned the information while repeating it will mirror the progressions to the dataset.
Now let’s see the modes of the resultset as follows.
Basically, there are two modes of concurrency in the resultset as follows.
CONCUR_READ_ONLY: This is the first mode, and it is a default concurrency mode; in this mode, we can only read the data from the ResultSet. Here updating is not allowed.
CONCUR_UPDATABLE: The name suggests it allows us to update the data in the ResultSet as per user requirements.
Java provides the different methods for the ResultSetInterface we can use as per user requirements as follows.
1. Navigational Methods for ResultSet
In Navigation, the method cursor moves around the database, allowing us to perform the following methods as follows.
beforeFirst(): It allows you to move the cursor before the starting row from the database.
afterLast(): It allows you to move the cursor before the last row.
Boolean first(): It allows moving the cursor before the first row.
Void last(): It allows moving the cursor before the last row.
Also, we have more methods we can utilize as per user requirements.
2. Get Methods for ResultSet
ResultSet has put away the information of the table from the Database. Get methods are utilized to get the upsides of the table in ResultSet. For that, we need to pass either segment Index worth or Column Name.
It provides the different methods that we listed below as follows.
getint: Basically, it is used to get the value from the specified column index with the int data type that we want.
getFloat: Basically, it is used to get the value from the specified column index with a float data type that we want.
It also provides different methods such as java.sql.data getData, int getint, etc.
3. Update methods for ResultSet
We can update the worth in the Database utilizing ResultSet Updater strategies. It is like Get techniques; however, we need to pass the qualities/information for the specific segment to refresh the Database. It provides the following methods that we listed below as follows.
Updateint, updatefloat, updatedate etc.
Examples of JDBC resultset
Now let’s see the different examples of ResultSet for better understanding as follows.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSetExample {
public static void main(String[] args) {
Connection con_obj = null;
Statement stmt_obj = null;
try {
String m_url = " jdbc:mysql://localhost ";
Connection con = DriverManager.getConnection(m_url, "root", "root");
statement = con_obj.createStatement();
String query_stmt = "SELECT * FROM stud";
ResultSet r_set_obj = stmt_obj.executeQuery(query_stmt);
while (r_set_obj.next()) {
System.out.println("Data from database...");
String stud_Name = r_set_obj.getString("stud_name");
String Dept = r_set_obj.getString("dept");
int marks = r_set_obj.getInt("marks");
System.out.println("\tName: " + stud_Name + ", Dept: " + Dept + ", Marks: " + marks);
}
} catch (ClassNotFoundException e_obj) {
E_obj.printStackTrace();
} catch (SQLException e_obj) {
E_obj.printStackTrace();
} finally {
try {
stmt_obj.close();
} catch (SQLException e_obj) {
e_obj.printStackTrace();
}
try {
con_obj.close();
} catch (SQLException e_obj) {
e_obj.printStackTrace();
}
}
}
}
Explanation
The final output of the above program we illustrated by using the following screenshot as follows.
Conclusion
We hope from this article you learn the JDBC ResultSet. From the above article, we have learned the basic syntax of ResultSet, and we also see different examples of JDBC ResultSet. Furthermore, from this article, we learned how and when we use the JDBC ResultSet.
Recommended Articles
This is a guide to JDBC resultset. Here we discuss the basic syntax of ResultSet, and we also see different examples of JDBC ResultSet. You may also have a look at the following articles to learn more –