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

时间:2022-07-09 11:53:13

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”的列中获取数据?

4 个解决方案

#1


72  

Use the ResultSetMetaData class.

使用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.

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

#2


4  

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

#3


-1  

/**
 * 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 version> = 7中,您可以选择在Re​​sultSet#getObject方法中传递Class类型

#4


-13  

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

如果不是rs.getString(“Column_ABC”)=什么,那么'你的代码在这里

#1


72  

Use the ResultSetMetaData class.

使用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.

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

#2


4  

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

#3


-1  

/**
 * 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 version> = 7中,您可以选择在Re​​sultSet#getObject方法中传递Class类型

#4


-13  

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

如果不是rs.getString(“Column_ABC”)=什么,那么'你的代码在这里