ResultSet

Java JDBC ResultSet Example

In this example, we are going to demonstrate how to use Java JDBC ResultSet in order to get and manipulate data from a database. ResultSet is essentially a table, which contains all the information that should be returned from a specific query, as well as some essential metadata.

You can also check this tutorial in the following video:

Java JDBC Tutorial – video

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. The ResultSet interface provides getter methods for retrieving column values from the current row. Values can be retrieved using the column name or the index number of the column. The usual syntax to get a ResultSet is as shown below:

Connection conn = DriverManager.getConnection(database specific URL);
Statement stmt = conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
ResultSet rSet = stmt.executeQuery("query to execute passed as String");
while(rSet.next()){
// Fetch the results and perform operations
...
}

A ResultSet is obtained after executing a query. Download NOW!

1
2
3
4
5
6
Printing result...
    Album: The White Album, by Artist: The Beatles, released in: 1968
Printing result...
    Album: The Black Album, by Artist: Metallica, released in: 1991
Printing result...
    Album: Rock in Rio, by Artist: Iron Maiden, released in: 2001

As stated above, the ResultSet class has different methods for acquiring different kinds of data, depending on the column type in the table. It is very important to know what kind of data you want to retrieve, because, in case of a mismatch, an exception will be thrown. For example, in the above code, if we change this line:

1
String albumName = resultSet.getString("name");

to this:

1
int albumName = resultSet.getInt("name");

we will immediately get an exception when we try to retrieve the first result.

1
2
3
4
5
6
7
8
9
Printing result...
java.sql.SQLException: Invalid value for getInt() - 'The White Album'
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2821)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2846)
    at ResultSetExample.main(ResultSetExample.java:51)

Generally, you need to be sure about the data types and use the right method. Some of the most common MySQL – Java relations are these:

  • Varchar -> getString(columnName): String
  • Char ->  getString(columnName): String
  • Integer  -> getInt(columnName): int
  • Float  -> getFloat(columnName): float
  • Boolean -> getBoolean(columnName): boolean
  • Date -> getDate(columnType): Date

In some cases, you can use the getString(columnName) method to get the String representation of numbers, like Integer or Float, but this course of action is better to be avoided.

6. Download the source code

This was an example of how to use JDBC ResultSet in Java.

Download
You can download the eclipse project of this tutorial here: 
Sign up
Tags

Ilias Koutsakis

Ilias has graduated from the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. He is interested in all aspects of software engineering, particularly data mining, and loves the challenge of working with new technologies. He is pursuing the dream of clean and readable code on a daily basis.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button