簡體   English   中英

oracle 11g resultSet如何獲取表名

[英]oracle 11g resultSet how to get the table name

我發現Oracle 11g中存在從接口獲取表名的問題(ResultSet.getMetaData().getTableName(int column));

它總是顯示空字符串。

oracle數據庫或jdbc驅動程序有什么問題嗎? 如果jdbc驅動程序的問題,我可以獲得另一個jdbc驅動程序來解決此問題嗎?

提前致謝!

根據文檔 ,不支持:

但是沒有實現getSchemaNamegetTableName方法,因為Oracle數據庫不能實現這一點

早期的Oracle驅動程序確實具有此功能,但由於其性能影響,需要明確啟用它。 據我所知,從文檔中可以看出,這在最近的驅動程序中已不再可用。

你可以使用:

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");
}

閱讀本文以獲取更多信息。

在幾天前遇到這個特殊問題后,我終於想出了一個能夠完成這項工作的解決方案。 當然它既不漂亮也不......好......任何東西,但它確實有效。

基本上,我根據ResultSet中的 檢查數據庫中的每個表

我希望其他人可以使用它。 我花了一天時間才能做到這一點。

注意:我使用CachedRowSet而不是ResultSet,這不需要我一直保持數據庫連接打開。



 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; 
}

周圍的方法:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM