Search code examples
javasqlitejdbcresultsetdatabase-metadata

How to return a scrollable result set from getMetaData.getColumns()?


I need to return a scrollable result set from the code below:

ResultSet columnMetaData = connection.getMetaData().getColumns(null, null, "my_table", "%");

However, the getColumns() API doesn't appear to provide a way for me to say what kind of result set type I'd like the ResultSet to be. It only returns a TYPE_FORWARD_ONLY result set. The only examples I've found for creating a scrollable result set involve creating a Statement. But I can only get a DatabaseMetaData object (and thereby call the getColumns method) from the Connection object, not a Statement.

Does anyone know of a way to return a scrollable result set from calling getColumns()?

By the way, the code above is used with xerial's SQLite JDBC Driver.


Solution

  • You can't. The JDBC specification explicitly specifies that metadata result sets need to be TYPE_FORWARD_ONLY. See JDBC 4.3 Specification, section 7.5 SQL Objects and Their Attributes:

    The ResultSet objects that are returned from a DatabaseMetaData method have a sensitivity of TYPE_FORWARD_ONLY and a concurrency of CONCUR_READ_ONLY. ResultSet.getHoldability can be called to determine the holdability of the returned ResultSet object as the default holdability is implementation defined.

    You don't explain why you need it to be scrollable, but you either need to transform it to a list, or maybe wrap it in a CachedRowSet.