How Do You Create a ResultSetMetaData Object? JDBC

ResultSetMetaData is a Java class, which provides information about the ResultSet and implements the java.sql.ResultSetMetaData interface. The column name, column type, and so forth are provided by the ResultSetMetaData object. Database vendors provide different classes for implementing the ResultSetMetaData interface. To learn about a given result set from a database (expressed as a ResultSet object), you must obtain (or create) a ResultSetMetaData object. Once a client program has obtained a valid ResultSet, the following code gets a metadata object:

ResultSet rs = null;
...
try {
...
rs = <get a result set>;
ResultSetMetaData rsMetaData = rs.getMetaData();
if (rsMetaData == null) {
// there is no metadata for a given result set
...
}
else {
// once you are here, then you can execute methods
// given in the ResultSetMetaData.
// get the number of columns for a given result set
int numberOfColumns = rsMetaData.getColumnCount();
}
...
}
catch(SQLException e) {
// handle the exception
}
finally {
// close the rs (result set object)
}

The ResultSetMetaData interface has over two dozen methods. Using ResultSetMetaData, an application can discover the number of columns returned, as well as an individual column’s suggested display size, column names, column types, and so on. Note that a given JDBC driver may not provide information for all of the methods, so you need to check returned objects for nulls or empty strings.

A ResultSet object provides a table of data representing a database result set, which is usually generated by executing a statement that queries the database. The ResultSet object is one of the most important objects in the JDBC API. ResultSet is basically an abstraction of a table of general width and unknown length. Most of the methods and queries return result data as a ResultSet. A ResultSet contains any number of named columns that you can ask for by name. It also consists of one or more rows, which you can move through sequentially from top to bottom, one at a time. Before you can use a ResultSet, you need to inspect the number of columns it contains. This information is stored in the ResultSetMetaData object.

//get the number of columns from the "ResultSet metadata"
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
...
try {
// get a result set
conn = ... get a java.sql.Connection object ...
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id, name FROM employees");
ResultSetMetaData rsmd = rs.getMetaData();
if (rsmd == null) {
// no result set metadata
...
}
else {
int numberOfColumns = rsmd.getColumnCount();
if (numberOfColumns != 2) {
// error
}
}
}
catch(SQLException e) {
// handle the exception
}
finally {
// close the rs (result set object)
// close the stmt (Statement object)
// close the conn (connection object)
}

When you obtain a ResultSet, it points just before the first row. To iterate all of the rows, you can use the next() method to obtain each additional row, and the method returns false when no more rows remain. Since fetching data from a database can result in errors, you must always enclose your result set manipulations in a try/catch block.

Here’s an example:

import java.sql.*;
import jcb.util.DatabaseUtil;
...
public static Connection getConnection()
throws Exception {
// return a valid database connection object
}
...
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id, name FROM employees");
ResultSetMetaData rsmd = rs.getMetaData();
if (rsmd == null) {
// ResultSetMetaData is not supported
}
else {
int numberOfColumns = rsmd.getColumnCount();
while(rs.next()) {
for (i = 1; i <= numberOfColumns; i++) {
System.out.println(rs.getString(i)+" ");
}
System.out.println("------");
}
}
}
catch(Exception e) {
// handle and deal with the exception
System.out.println(e.getMessage());
}
finally {
// close database resources
DatabaseUtil.close(rs);
DatabaseUtil.close(stmt);
DatabaseUtil.close(conn);
}

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics