RowSetMetaData

javax.sql.RowSetMetaData Example

RowSetMetaData is an object that contains information about the columns in a RowSet object. Some of these column properties are column’s table name, type, count, label, name, size, is nullable, is read only and so on.

This interface is an extension of the ResultSetMetaData interface with methods for setting the values in a RowSetMetaData object. The corresponding getter methods are inherited from the ResultSetMetaData.

From the API’s javadoc – The methods in this interface are invoked internally when an application calls the RowSet‘s execute() method; an application programmer would not use them directly.

1. An Example

The example program retrieves a rowset’s metadata, modifies it and verifies the changes.

A CachedRowSet is created and populated with a database’s table rows. The CachedRowSet‘s getMetaData() method is used to get the RowSetMetaData object. The metadata object is queried and some information about the columns is printed. Next, the metadata is changed and rowset is updated.

The following is the code of the example program and its output.

1.1. The Code

RowsetMetadataExample.java

import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.CachedRowSet;
import javax.sql.RowSetMetaData;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class RowsetMetadataExample {

    private final static String DB_URL = "jdbc:mysql://localhost:3306/example_db";
    private final static String USR = "root";
    private final static String PWD = "root";
    private final static String BOOKS_TABLE = "books_table";
    private final static String NEW_COLUMN_NAME_1 = "book_title";
    private final static String NEW_COLUMN_NAME_2 = "book_author";

    public static void main(String [] args)
            throws Exception {

        CachedRowSet crs = getCachedRowset();
        Connection con = getDataSourceConnection();
        populateRowset(crs, con);
        RowSetMetaData md = getMetaData(crs);
        setMetaData(crs, md);
        crs.close();
        System.out.println("Close.");
    }

    private static CachedRowSet getCachedRowset()
            throws SQLException {

        System.out.println("Create cached rowset");
        RowSetFactory rsFactory = RowSetProvider.newFactory();
        CachedRowSet rowset = rsFactory.createCachedRowSet();
        return rowset;
    }

    private static Connection getDataSourceConnection()
            throws SQLException {

        System.out.println("Get connection to database: " + DB_URL);
        MysqlDataSource myDs = new MysqlDataSource();
        myDs.setURL(DB_URL);
        DataSource ds = myDs;
        Connection con = ds.getConnection(USR, PWD);
        return con;
    }

    private static void populateRowset(CachedRowSet crs, Connection con)
            throws SQLException {

        System.out.println("Populate rowset with database table rows: " + BOOKS_TABLE);
        String sql = "SELECT * FROM " + BOOKS_TABLE;
        crs.setCommand(sql);
        crs.execute(con);
        crs.last();
        System.out.println("Total rows in table: " + crs.getRow());
    }

    private static RowSetMetaData getMetaData(CachedRowSet crs)
            throws SQLException {

        RowSetMetaData metaData = (RowSetMetaData) crs.getMetaData();

        System.out.println("Row set meta data: ");
        int cols = metaData.getColumnCount();
        System.out.println("  Column count: " + cols);

        for (int i = 1; i <= cols; i++) {

            System.out.println("  Column: " + i);
            System.out.println("    Table: " + metaData.getTableName(i));
            System.out.println("    Column name: " + metaData.getColumnName(i));
            System.out.println("    Column type: " + metaData.getColumnTypeName(i));
            System.out.println("    Column is nullable (0=No Nulls, 1=Nullable): " + metaData.isNullable(i));
        }

        return metaData;
    }

    private static void setMetaData(CachedRowSet crs, RowSetMetaData metaData)
            throws SQLException {

        metaData.setColumnName(1, NEW_COLUMN_NAME_1);
        metaData.setColumnName(2, NEW_COLUMN_NAME_2);
        crs.setMetaData(metaData);

        metaData = (RowSetMetaData) crs.getMetaData();
        System.out.println("Row set meta data - with updated column names: ");
        System.out.println("    Column 1 name: " + metaData.getColumnName(1));
        System.out.println("    Column 2 name: " + metaData.getColumnName(2));
    }
}

NOTE: The code accesses a database table from a MySQL server. The MySQL database driver program is required to be in the classpath for compiling the code.

The Output

Create cached rowset
Get connection to database: jdbc:mysql://localhost:3306/example_db
Populate rowset with database table rows: books_table
Total rows in table: 5
Row set meta data:
  Column count: 2
  Column: 1
    Table: books_table
    Column name: title
    Column type: VARCHAR
    Column is nullable (0=No Nulls, 1=Nullable): 0
  Column: 2
    Table: books_table
    Column name: author
    Column type: VARCHAR
    Column is nullable (0=No Nulls, 1=Nullable): 1
Row set meta data - with updated column names:
    Column 1 name: book_title
    Column 2 name: book_author
Close.

From the output, note that there are two columns in the rowset (populated from the “books_table”) and the column properties are shown. After the metadata is changed, the updated values of column name property are shown (for example, the column name “title” is changed to “book_title”).

NOTE: The CachedRowset interface has the setMetaData() method (shown on line 88 of the code above), and this is inherited by all its sub-interfaces. The only rowset without this method is the JdbcRowSet.

2. Download Java Source Code

This was an example of javax.sql.RowSetMetaData

Download
You can download the full source code of this example here: RowsetMetadataExample.zip

Prasad Saya

Prasad Saya is a software engineer with over ten years’ experience in application development, maintenance, testing and consulting on various platforms. He is a certified Java and Java EE developer. At present his interest is in developing Java applications. He also has experience working with databases and ERP applications.
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