Convert a Result Set from SQL Array to Array of St

2020-01-30 11:52发布

I am querying the information_schema.columns table in my PostgreSQL database. Using a table name, the result set finds all the column names, type, and whether it is nullable (except for the primary key, 'id'). This is the query being used:

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;

I have a string array for each of these results and I am trying to use the ResultSet method getArray(String columnLabel) to avoid looping through the results. I want to store the returned Arrays in the string arrays, but get a type mismatch error

Type mismatch: cannot convert from Array to String[]

Is there a way to convert or typecast the SQL Array object to a String[]?

Relevant Code:

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

5条回答
beautiful°
2楼-- · 2020-01-30 12:31
Object[] balance = (Object[]) tableObject.getArray();

here table Object is a Table Type array coming from DB procedure call in dao implmentation. after we need to parse it.

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);
         }
   }
}
查看更多
在下西门庆
3楼-- · 2020-01-30 12:33

How to set an ArrayList property from an SQL Array:

Array a = rs.getArray("col"); // smallint[] column
if (a != null) {
    yourObject.setListProperty(Arrays.asList((Integer[]) a.getArray()));
}
查看更多
劫难
4楼-- · 2020-01-30 12:38

Use:

Array a = rs.getArray("is_nullable");
String[] nullable = (String[])a.getArray();

As explained here

Array is SQL type, getArray() returns an object to cast to java array.

查看更多
甜甜的少女心
5楼-- · 2020-01-30 12:45

this can be helpful

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

            }

        }
查看更多
何必那么认真
6楼-- · 2020-01-30 12:47

Generalize the Array to Object

    Object[] type; //this is generic can use String[] directly
    Array rsArray;

    rsArray = rs.getArray("data_type");
    type = (Object [])rsArray.getArray();

Use it loop as string:

type[i].toString();
查看更多
登录 后发表回答