HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

8) SQL & JDBC Lesson

What is the Java ResultSet?

5 min to complete · By Ryan Desmond

TheResultSet represents a table of data holding the results of a database query.

How Does a ResultSet Read Data

When reading a table of data, the ResultSet starts at the first row by placing a cursor, which will mark the current row. The next() method is used to move the cursor forward in the table and will return false when there are no further rows available. This allows you to use a while loop to easily iterate through the ResultSet.

The default ResultSet cannot be updated, and the cursor can only move forward, not backward. Technically, you can update the values inside a ResultSet, but it's a very rare case and not worth exploring at the moment.

It is possible to retrieve data from the ResultSet object using both ordinal positions (i.e., 1 or 2 for the 1st column or 2nd column) as well as by the column title (i.e., Emp_Name).

ResultSet Built-in Methods

ResultSet objects have several dozen methods available for you to use to access and interact with the data returned from the database query. Take a quick look at the ResultSet JavaDoc to get an idea of just how many methods there are. You also get an idea of it from the image below (in the left column), which shows just a portion of the available methods.

How to Access Row Elements

The code below shows you how to access rows using result sets.

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://localhost/college?" +
  "user=<USERNAME>&password=<PASSWORD>&useSSL=false");

statement = connection.createStatement();  
resultSet = statement.executeQuery("select * from college.courses;");  

while (resultSet.next()) {
  // It is possible to get the columns via name, and it is
  // It is also possible to get the columns via the "ordinal" 
  // column number, which starts at 1
  int id = resultSet.getInt("id"); 
  String course = resultSet.getString("name");
  int credits = resultSet.getInt("units");
  System.out.println("Course ID: " + id  +  " is " 
    + course + " for " + credits + "credits");
  System.out.println("---------------------------------");
}

Access ResultSet MetaData

Using a ResultSet object, it is possible to iterate through the data as well as the metadata. In the image below, you can see a snapshot of the methods available to us (in the left column navigation) in the ResultSetMetaData object.

The code sample below accesses the metadata.

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://localhost/college?" +
    "user=<USERNAME>&password=<PASSWORD>&useSSL=false");

statement = connection.createStatement();  
resultSet = statement.executeQuery("select * from college.courses;");  

System.out.println("Table: " 
  + resultSet.getMetaData().getTableName(1));  
System.out.println("The columns in the table are: ");

for  (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
  System.out.println("Column " +i  + " "+  
    resultSet.getMetaData().getColumnName(i));
}

Summary: What is the Java ResultSet

  • The ResultSet is an interface in Java for iterating through table data
  • The next() is used to move the ResultSet cursor to the next row
  • The default ResultSet cannot be used to update data
  • The ResultSetMetaData stores the metadata of the ResultSet
  • Both the ResultSet and the ResultSetMetaData come with many built-in methods to access the data