Convert a Result Set from SQL Array to Array of Strings

前端 未结 5 1962
鱼传尺愫
鱼传尺愫 2020-12-02 22:43

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 i

相关标签:
5条回答
  • 2020-12-02 23:22
    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);
             }
       }
    }
    
    0 讨论(0)
  • 2020-12-02 23:31

    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);
                    }
    
                }
    
            }
    
    0 讨论(0)
  • 2020-12-02 23: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.

    0 讨论(0)
  • 2020-12-02 23:43

    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();
    
    0 讨论(0)
  • 2020-12-02 23:43

    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()));
    }
    
    0 讨论(0)
提交回复
热议问题