
Java JDBC ResultSet Example
- 1. Why we use ResultSet interface A ResultSet is a table of data representing a database result set, which is usually generated by executing a statement that queries the database. ...
- 2. Categories in ResultSet ...
- 3. Technologies used ...
- 4. Creating a new project and adding the driver to the build path ...
- 5. Java JDBC ResultSet Example ...
- 6. Download the source code
What is resultsetmetadata in JDBC?
What is ResultSetMetaData? - ResultSetMetaData is a class which provides information about a result set that is returned by an executeQuery () method. JDBC details interrogation can be done by using ResultSetMetaData. It includes the information about the names of the columns, number of columns, data type they contain etc.
How to execute any type of query in JDBC?
What are the steps required to execute a query in JDBC?
- Import JDBC packages.
- Load and register the JDBC driver.
- Open a connection to the database.
- Create a statement object to perform a query.
- Execute the statement object and return a query resultset.
How to update a record in the database using JDBC?
Update Data using JDBC program
- Register the driver. To communicate with the database, first of all, you need to register the driver. ...
- Get the connection. In general, the first step we do to communicate to the database is to connect with it. ...
- Create a statement object. ...
- Execute the query. ...
What are the steps in the JDBC connection?
While making a JDBC connection we go through the following steps :
- Register the database driver by using : Class.forName (\" driver classs for that specific database\" );
- Now create a database connection using : Connection con = DriverManager.getConnection (url,username,password);
- Now Create a query using : Statement stmt = Connection.Statement (\"select * from TABLE NAME\");
- Exceute the query :

What is ResultSet in JDBC in Java?
The java. sql. ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object.
What are the types of ResultSet in JDBC?
There are two types of result sets namely, forward only and, bidirectional.
What is a ResultSet in SQL?
An SQL result set is a set of rows from a database, as well as metadata about the query such as the column names, and the types and sizes of each column. Depending on the database system, the number of rows in the result set may or may not be known.
How do you declare a ResultSet in Java?
ResultSet interfaceStatement stmt = con. createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE,ResultSet. CONCUR_UPDATABLE);
What are different types of result sets?
ResultSet Types1) Forward Only (ResultSet. TYPE_FORWARD_ONLY)2) Scroll Insensitive (ResultSet. TYPE_SCROLL_INSENSITIVE)3) Scroll Sensitive (ResultSet. TYPE_SCROLL_SENSITIVE)
Can we return ResultSet in Java?
Yes, just like any other objects in Java we can pass a ResultSet object as a parameter to a method and, return it from a method.
What is a ResultSet in database?
The result set is an object that represents a set of data returned from a data source, usually as the result of a query. The result set contains rows and columns to hold the requested data elements, and it is navigated with a cursor.
What is the meaning of ResultSet?
A ResultSet is a Java object that contains the results of executing an SQL query. In other words, it contains the rows that satisfy the conditions of the query. The data stored in a ResultSet object is retrieved through a set of get methods that allows access to the various columns of the current row. The ResultSet.
How do you get ResultSet?
executeQuery method to obtain the result table from the SELECT statement in a ResultSet object. In a loop, position the cursor using the next method, and retrieve data from each column of the current row of the ResultSet object using getXXX methods. XXX represents a data type. Invoke the ResultSet.
How do you set values in ResultSet?
ProcedureCall the ResultSet. moveToInsertRow method to create the row that you want to insert. The row is created in a buffer outside the ResultSet. ... Call ResultSet. updateXXX methods to assign values to the row that you want to insert. ... Call ResultSet. insertRow to insert the row into the ResultSet.
What is the default type of ResultSet in JDBC application?
TYPE_FORWARD_ONLYThe default ResultSet type is TYPE_FORWARD_ONLY . Note: Not all databases and JDBC drivers support all ResultSet types. The method DatabaseMetaData.
How do I convert a ResultSet to a list?
You need to use ResultSet#getString to fetch the name . Now, each time you fetch one record, get the name field and add it to your list. Now, since you haven't given enough information about your DTO , so that part you need to find out, how to add this ArrayList to your DTO .
What are the types of ResultSet in JDBC Mcq?
Based on the concurrency there are two types of ResultSet object. CONCUR_READ_ONLY: The ResultSet is read-only, this is the default concurrency type. CONCUR_UPDATABLE: We can use the ResultSet update method to update the rows data.
What is the default type of the ResultSet in JDBC?
TYPE_FORWARD_ONLYThe default ResultSet type is TYPE_FORWARD_ONLY . Note: Not all databases and JDBC drivers support all ResultSet types. The method DatabaseMetaData.
What are the different types of JDBC statements?
There are three types of statements in JDBC namely, Statement, Prepared Statement, Callable statement.
What is forward only ResultSet?
In forward only ResultSet you can move the cursor only in forward direction. By default, a ResultSet is of type forward only.
1. Why we use ResultSet interface
A ResultSet is a table of data representing a database result set, which is usually generated by executing a statement that queries the database. A ResultSet object maintains a cursor pointing to its current row of data, initially positioned before the first row.
3. Technologies used
For the purposes of this article, we are going to assume that the database in use is MySQL, since it is one of the most well-known and beginner-friendly databases out there. In general, we are using:
4. Creating a new project and adding the driver to the build path
First of all download the JDBC driver needed for your database. In our case, you will need the MySQL Connector, which can be found and downloaded here. Select the Platform Independent option, and download the zip file which contains, among others, the MySQL Connector jar file which will be added in the build path.
5. Java JDBC ResultSet Example
There are certain steps to be taken in order to use ResultSet in Java:
What is JDBC resultset?
JDBC ResultSet interface is used to store the data from the database and use it in our Java Program.
What is result set in Java?
It is used to store the data which are returned from the database table after the execution of the SQL statements in the Java Program. The object of ResultSet maintains cursor point at the result data. In default, the cursor positions before the first row of the result data.
What does Boolean last mean?
Boolean last (): It makes the ResultSet cursor to move to the last row. It returns True if the operation is successful else False.
How many categories of ResultSet methods are there?
There are 4 categories of ResultSet methods. They are:
What does void moveToCurrentRow do?
Void moveToCurrentRow (): It moves the cursor back to the current row if it is currently in insert row.
What is boolean absolute?
Boolean absolute (int row): It is used to move the cursor to the specified row which is mentioned in the parameter and return true if the operation is successful else return false.
What is type_forward_only?
TYPE_FORWARD_ONLY: It is the default option, where the cursor moves from start to end i.e. in the forward direction.

How Does The Resultset Work in JDBC?
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 =...
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 – 1. JDBC Driver for Oracle 2. JDBC Driver 3. PostgreSQL JDBC Driver 4. What is JDBC?
Why We Use Resultset Interface
Categories in Resultset
Technologies Used
Creating A New Project and Adding The Driver to The Build Path
Java JDBC Resultset Example
- There are certain steps to be taken in order to use ResultSet in Java: 1. Load the MySQL driver to your program. 2. Create a Connectionto the database. 3. Make a query using a Statement. 4. Get the ResultSetand manipulate the data as needed. For this example, we assume that we have a local database running, named “albums“, which contains a table na...