【问题标题】:oracle 11g resultSet how to get the table nameoracle 11g resultSet如何获取表名
【发布时间】:2014-01-06 08:50:47
【问题描述】:

我发现Oracle 11g从接口(ResultSet.getMetaData().getTableName(int column));获取表名有问题

它总是显示空字符串。

oracle 数据库或 jdbc 驱动程序有问题吗?如果 jdbc 驱动有问题,我可以换一个 jdbc 驱动来解决这个问题吗?

提前致谢!

【问题讨论】:

  • 能否显示代码,ResultSet接口没有getTableName download.java.net/jdk8/docs/api/java/sql/ResultSet.html
  • 哎呀,我重新编辑我的帖子!
  • getTableName() 根本没有在 Oracle JDBC 驱动程序中实现。
  • 尽可能显示完整代码?
  • 我好像走进了死胡同。感谢你的帮助!顺便说一句,我爱mysql!

标签: java sql jdbc oracle11g


【解决方案1】:

根据documentation,这是不支持的:

但没有实现 getSchemaNamegetTableName 方法,因为 Oracle 数据库无法做到这一点

Earlier Oracle drivers 确实具有此功能,但由于其性能影响,需要显式启用。据我从文档中可以看出,这在更新的驱动程序中不再可用。

【讨论】:

  • 非常感谢!你为我节省了很多时间来解决这个问题......我想我应该绕过这个问题。我没有足够的声望来投票给你!很抱歉。
【解决方案2】:

【讨论】:

  • 好像很无奈,我用getCatalogName(int column);也返回空字符串。
【解决方案3】:

你可以使用:

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

阅读this了解更多信息。

【讨论】:

  • 这不能替代ResultSetMetaData.getTableName() - getTableName() 应该返回查询列的基础表。 getTables() 不会帮助您找出查询列属于哪个表。
  • 同意。但正如您所提到的,getTableName() 根本没有在 Oracle JDBC 驱动程序中实现。这效率不高,但循环遍历表及其列可能是唯一的方法。我希望有人发布更好的方法来做到这一点。
【解决方案4】:

在几天前遇到这个具体问题后,我终于想出了一个可以完成这项工作的解决方案。当然,它既不漂亮也不......好吧......任何东西,但它有效。

基本上,我根据 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;
}

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 2013-11-02
    • 1970-01-01
    • 2021-07-05
    • 2022-10-12
    • 2011-11-05
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多