将结果集从SQL数组转换为字符串数组

我正在查询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(); 

如此处所述

Array是SQL类型, getArray()返回一个要转换为java数组的对象。

将Array推广到Object

  Object[] type; //this is generic can use String[] directly Array rsArray; rsArray = rs.getArray("data_type"); type = (Object [])rsArray.getArray(); 

使用它作为字符串循环:

 type[i].toString(); 

如何从SQL数组设置ArrayList属性:

 Array a = rs.getArray("col"); // smallint[] column if (a != null) { yourObject.setListProperty(Arrays.asList((Integer[]) a.getArray())); } 

这可能会有所帮助

 Object[] balance = (Object[]) ((Array) attributes[29]).getArray(); for (Object bal : balance) { Object [] balObj =(Object[]) ((Array) bal).getArray(); for(Object obj : balObj){ Struct s= (Struct)obj; if(s != null ){ String [] str = (String[]) s.getAttributes(); System.out.println(str); } } }