我正在查询我的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数组对象转换或强制类型转换为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();
}
rs.getArray("is_nullable").getArray()
,但我认为我没有进行类型转换。这似乎解决了我的问题。 - Mattjava.sql.Array.getArray()
方法现在什么也不会做,只会抛出一个SQLFeatureNotImplemented
异常,因此我们必须寻找另一种方法。 - Cameron Hudson