Iterating metadata.getColumnCount()

466 views Asked by At

Basically I am trying to read multiple queries from excel sheet through jexel and I am adding the cell contents to Key and Value in hashmap. For example

A1=1 (Key) , A2 =2 ....

B1 = "SELECT * FROM ABCD;" (Value), B2 = "SELECT * FROM XYZ;"

Map<String, String> hashmap = new HashMap<String, String>();
for (int i=0;i<rowCount;i++) {
    Cell rowObj1 =sheet.getCell(0, i);      
    Cell rowObj2 = sheet.getCell(1, i);
    sql =rowObj2.getContents();
    hashmap.put(rowObj1.getContents(), rowObj2.getContents());
}

Later I am getting the SQL(s) from the hashmap by iterating over rowCount on exel sheet. Then I am adding all SQL(s) result sets to an array list and finally I am iterating over array list size to get individual record set, at the same time get metadata for the record set.

However the problem here is I get

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:418)
at java.util.ArrayList.get(ArrayList.java:431)
at com.bmc.arsys.jdbc.core.ResultSetMetaData.getColumnCount(ResultSetMetaData.java:47)

on

int ColCount = metadata1.getColumnCount();

and here is the code snippet

ArrayList<ResultSet> arraylist = new ArrayList<ResultSet>();
int i;
String y;
for (i=1;i<=rowCount;i++){
    y = Integer.toString(i);
    sql =hashmap.get(y);
    rs  = st.executeQuery(sql);
    arraylist.add(rs);
    System.out.println(sql);
}

System.out.println("Array List Size "+arraylist.size());
for (int j=0;j<arraylist.size();j++){
    ResultSet rs1= arraylist.get(j);
    System.out.println("Record Set "+rs1);

    ResultSetMetaData metadata1 = rs1.getMetaData();
    System.out.println("Meta Data "+metadata1);
    int ColCount = metadata1.getColumnCount();
    System.out.println(ColCount);

    int k;
    for (k=1;k<ColCount;k++) {
        System.out.println("Record_Set "+rs1);
        while(rs1.next()) {
            System.out.println(rs1.getString(k));
        }   
    }
}

could someone please suggest where I am wrong.

0

There are 0 answers