简体   繁体   中英

oracle 11g resultSet how to get the table name

I found there is some problem in Oracle 11g to get table name from interface (ResultSet.getMetaData().getTableName(int column));

It always show the empty string.

Is there something wrong for oracle database or jdbc driver? If the jdbc driver's problem , Can I get another jdbc driver to resolve this issue?

Thanks in advance!

According to the documentation this is not supported:

but does not implement the getSchemaName and getTableName methods because Oracle Database does not make this feasible

Earlier Oracle drivers did have this feature, but it needed to be enabled explicitly because of its performance implications. As far as I can tell from the documentation this is no longer available in more recent drivers.

You could use:

DatabaseMetaData  metadata = currentConnection.getMetaData();
String[] names = {"TABLE"}; 
ResultSet tables = metadata.getTables(null,"%", "%", names);
while (tables.next()) { 
  String tableName = tables.getString("TABLE_NAME"); 
  String tableSchema = tables.getString("TABLE_SCHEM");
}
ResultSet columns = metadata.getColumns(null, "%", tableName, "%");
while (columns.next()) { 
  String columnName = columns.getString("COLUMN_NAME"); 
  String datatype = columns.getString("TYPE_NAME"); 
  int datasize = columns.getInt("COLUMN_SIZE"); 
  int nullable = columns.getInt("NULLABLE");
}

Read this for more info.

after running into this specifical issue face-first a few days ago, I finally came up with a solution that does the job. Of course it's neither pretty nor... well... anything, but it works.

Basically, I check every table in my database against the columns in the ResultSet.

I hope someone else can use this. Took me about a day to get this straight.

Note: I use a CachedRowSet instead of a ResultSet, which doesn't require me to keep the database connection open the whole time.



 private static String getTableNameByCols(ResultSetMetaData rsmd, DatabaseMetaData dbmd) throws SQLException{

    String errorString = "No matching table found for the given column Set";
    String ret = null, origColName, origDatatype, tableName; 
    String[] names = {"TABLE"}; 

    ResultSet tables = dbmd.getTables(null, username, "%", names);

    // get all the columns out of the rsmd and put them into an Array
    Integer numberOfColumns = rsmd.getColumnCount();
    String[] origColNames = new String[numberOfColumns+1];
    String[] origColTypeNames = new String[numberOfColumns+1];

    for (int i=1; i<numberOfColumns+1; i++){
        origColNames[i] = rsmd.getColumnName(i);
        origColTypeNames[i] = rsmd.getColumnTypeName(i);
    }

    ResultSet columns = null;
    while (tables.next()) { 

        tableName = tables.getString("TABLE_NAME"); 
        columns = dbmd.getColumns(null, null, tableName, null);
        CachedRowSet crs = new CachedRowSetImpl();
        crs.populate(columns);

        Integer tablesNumberOfColumns = crs.size();

        int i = 1; 

        if (numberOfColumns.intValue() == tablesNumberOfColumns.intValue()){

            while (crs.next()) {

                origColName = origColNames[i];
                origDatatype = origColTypeNames[i];

                String colName = crs.getString(4); 
                String datatype = crs.getString(6); 
                //int datasize = columns.getInt("COLUMN_SIZE"); 
                //int nullable = columns.getInt("NULLABLE");
                if (origColName.equals(colName) && origDatatype.equals(datatype) ){
                    ret = tableName;
                } else {
                    ret = null; 
                }
                i += 1;

            } // looked at all the columns
            crs.close();

        }// same # of columns check over

        if (ret != null) {
            break;
        }

        columns.close();    
    }

    verify(ret, errorString);
    return ret; 
}

The surrounding method:

private static boolean updateLocalTable(ResultSet rs){

    ResultSetMetaData rsmd;
    DatabaseMetaData dbmd;
    String table_name;
    boolean ret = false; 
    try {
        rsmd = rs.getMetaData();
        dbmd = conn.getMetaData();
        table_name = getTableNameByCols(rsmd, dbmd);

        /* ... do stuff with it ... */

    } catch (Exception e) { 
        print("kablooey! \n" + e.getStackTrace());
    }

    return ret;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM