Java 如何确定 ResultSet 中是否存在列名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3599861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:28:53  来源:igfitidea点击:

How can I determine if the column name exist in the ResultSet?

javajdbcresultset

提问by Ken Chan

As the ResultSet contains the data returned from the dynamic SQL, if there are any method to determine if the ResultSet contains a particular column name ? For example , if I run rs.getString("Column_ABC");but Column_ABC does not really exist, it will throw out the exception . How can I test if the ResultSet can get a data from a column named "Column_ABC" ?

由于 ResultSet 包含从动态 SQL 返回的数据,是否有任何方法可以确定 ResultSet 是否包含特定列名?例如,如果我运行rs.getString("Column_ABC");但 Column_ABC 并不真正存在,它将抛出异常。如何测试 ResultSet 是否可以从名为“Column_ABC”的列中获取数据?

采纳答案by Erick Robertson

Use the ResultSetMetaDataclass.

使用ResultSetMetaData类。

public static boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int columns = rsmd.getColumnCount();
    for (int x = 1; x <= columns; x++) {
        if (columnName.equals(rsmd.getColumnName(x))) {
            return true;
        }
    }
    return false;
}

The thing I don't understand is why this function would ever be needed. The query or stored procedure being executed should have known results. The columns of the query should be known. Needing a function like this may be a sign that there is a design problem somewhere.

我不明白的是为什么需要这个功能。正在执行的查询或存储过程应该有已知的结果。查询的列应该是已知的。需要这样的功能可能表明某处存在设计问题。

回答by Amit

if not rs.getString("Column_ABC")= nothing then ' your code here

如果不是 rs.getString("Column_ABC")= nothing 那么 ' 你的代码在这里

回答by Zip184

Not sure if this is more or less efficient than Erick's answer but it's easier.

不确定这是否比 Erick 的答案更有效率或更低,但它更容易。

String str;

try {
    str = rs.getString(columnName);
} catch (java.sql.SQLException e) {
    str = null;
}

回答by VivekJ

/**
 * returns default value if column is not present in resultset
 * 
 * @param rs
 * @param columnLabel
 * @param defaultValue
 * @return
 */
@SuppressWarnings("unchecked")
private static <T> T getValueFromResultSet(final ResultSet rs,
        String columnLabel, T defaultValue) {
    try {
        return (T) rs.getObject(columnLabel);
    } catch (SQLException e) {
        return defaultValue;
    }
}

In java version >=7 you have a option of passing Class type in ResultSet#getObject method

在 java 版本 >=7 中,您可以选择在 ResultSet#getObject 方法中传递 Class 类型

回答by Talick

private boolean isThere(ResultSet rs, String column){
    try{
        rs.findColumn(column);
        return true;
    } catch (SQLException sqlex){
        logger.debug("column doesn't exist {}", column);
    }

    return false;
}

回答by sanket patwardhan

resultSet.getColumnMetaData().contains(columnName)

resultSet.getColumnMetaData().contains(columnName)