0

I am trying to build a DatabaseTable (my custom object) object by querying using jdbc DatabaseMetadata and ResultSet.

Below code works perfectly fine if I run it against MySQL database, but it fails with exception when tried against Exasol database.

 private DatabaseTableColumnMetadata getTableAndColumnNames(DatabaseMetaData metaData)
        throws SQLException {
        ResultSet tables = metaData.getTables(null,null,"%",null);
        while (tables.next()){
            if (tables.getString("TABLE_TYPE").equalsIgnoreCase("TABLE")
                && ((ResultSetImpl) tables).getConnection().getCatalog()
                .equals(tables.getString("TABLE_CAT"))) {

                     DatabaseTable table = DatabaseTable.builder().name(tables.getString("TABLE_NAME")).schema(tables.getString("TYPE_SCHEM")).build();
             }
        }
}

Exception thrown is as below

com.exasol.jdbc.EXAResultSet cannot be cast to com.mysql.cj.jdbc.result.ResultSetImpl","message":"com.exasol.jdbc.EXAResultSet cannot be cast to com.mysql.cj.jdbc.result.ResultSetImpl","name":"java.lang.ClassCastException

The exception thrown is at line where its trying to cast tables object to ResultSetImpl.

I have both jars in my project exajdbc.jar as well as mysql-connector.jar

Any help or clue to solve this problem pls.

  • Maybe instead of doing this filtering, you should pass the catalog as the appropriate parameter to [`getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/DatabaseMetaData.html#getTables(java.lang.String,java.lang.String,java.lang.String,java.lang.String[])) – Mark Rotteveel Feb 27 '20 at 18:20

1 Answers1

1

I'm not sure why you've written this expression:

     ((ResultSetImpl) tables).getConnection().getCatalog()

In this expression you are casting tables to a MySQL-specific result-set implementation class, just so that you can get access to its getConnection() method. It's perhaps not surprising you're getting an error attempting to use this code to read data from an Exasol database, because in this situation tables will be an Exasol-specific result-set implementation class.

Surely this result set comes from the same database connection as the DatabaseMetaData object that gets passed into your method? I would expect the getConnection() method of the DatabaseMetaData object to return the same connection.

Try replacing the expression above with the following:

     metaData.getConnection().getCatalog()
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104