我在PostgreSQL数据库中查询information_schema.columns表。结果集使用表名查找所有列名,类型以及它是否可为空(主键“id”除外)。这是正在使用的查询:

SELECT column_name, is_nullable,data_type FROM information_schema.columns
WHERE lower(table_name) = lower('TABLE1') AND column_name != 'id'
ORDER BY ordinal_position;

对于每个结果,我都有一个字符串数组,并且我尝试使用ResultSet方法 getArray(String columnLabel) 避免循环遍历结果。我想将返回的数组存储在字符串数组中,但是出现类型不匹配错误

Type mismatch: cannot convert from Array to String[]

有没有一种方法可以将SQL Array对象转换或转换为String []?

相关代码:

String[] columnName, type, nullable;

//Get Field Names, Type, & Nullability
String query = "SELECT column_name, is_nullable,data_type FROM information_schema.columns "
        + "WHERE lower(table_name) = lower('"+tableName+"') AND column_name != 'id' "
        + "ORDER BY ordinal_position";

try{
    ResultSet rs = Query.executeQueryWithRS(c, query);
    columnName = rs.getArray(rs.getArray("column_name"));
    type = rs.getArray("data_type");
    nullable = rs.getArray("is_nullable");
}catch (Exception e) {
    e.printStackTrace();
}

最佳答案

利用:

Array a = rs.getArray("is_nullable");
String[] nullable = (String[])a.getArray();
here所述Array是SQL类型,getArray()返回要转换为java数组的对象。

10-04 12:35
查看更多