0

Can anyone tell me what I am doing wrong here? I run this about 100 times, and about 2-8 times it fails with error: java.lang.ArrayIndexOutOfBoundsException: 10

public String[][] queryResult(String QUERY) throws SQLException{

    Connection con = getPoolConnection();
    Statement st2 = con.createStatement();
    ResultSet rs = st2.executeQuery(QUERY);
    System.out.println("Run query: "+QUERY);
    // outPrint(logFile,"Run query: "+QUERY+"");
    ResultSetMetaData metaData = rs.getMetaData();


    int noOfColumns = metaData.getColumnCount();
    //System.out.print(metaData.toString()+" : ");
    this.columnName = new String[noOfColumns];
    this.columnTypes = new String[noOfColumns];
    for (int i = 1; i <= noOfColumns; i++) {
        this.columnName[i-1] = metaData.getColumnLabel(i);
        this.columnTypes[i-1] = metaData.getColumnTypeName(i);
        System.out.print(this.columnName[i-1]+" - "+this.columnTypes[i-1]+" , ");

    }
    System.out.println(" - ");
    rs.last();
    int noOfRows = rs.getRow();
    rs.beforeFirst();

    String[][] result = new String[noOfRows][noOfColumns];

    int loop = 0;
    while(rs.next()) {
        loop++;
        for (int i = 1; i <= noOfColumns; i++) {
            result[loop-1][i-1] = getResultSwitch(metaData.getColumnType(i), rs, i);
            //System.out.println("result "+this.columnTypes[i-1]+" : "+result[loop-1][i-1]);
            System.out.print(result[loop-1][i-1]+" , ");
        }
        System.out.println(" - ");
    }

    rs.close();
    st2.close();
    con.close();
    //System.out.println("Closed connection.");

    return result;
}
2
  • Can you: provide the code of getResultSwitch and give us the line number where the exception is raised? Little advice, reuse your data: replace getResultSwitch(metaData.getColumnType(i) ... by getResultSwitch(this.columnTypes[i] ... Commented Sep 30, 2011 at 13:40
  • What is the line number where exception is raised? Provide some lines of your stacktrace, and can you reformat nested method calls with variable definitions to find the root of exception Commented Aug 18, 2013 at 4:59

2 Answers 2

0

Instead of using rs.last() and rs.getRow() to make a static array, I would suggest instead that you make an ArrayList of result rows, and convert it into an array when you're done.

 System.out.println(" - ");
    //rs.last();
    //int noOfRows = rs.getRow();
    //rs.beforeFirst();

    //String[][] result = new String[noOfRows][noOfColumns];
    List<String[]> tempResult = new ArrayList<String[]>();

    //int loop = 0;
    while(rs.next()) {
        //loop++;
       String[] row = new String[noOfColumns];
        for (int i = 0; i < noOfColumns; i++) {
            row[i] = getResultSwitch(metaData.getColumnType(i+i), rs, i);
            //System.out.println("result "+this.columnTypes[i-1]+" : "+result[loop-1][i-1]);
            //System.out.print(result[loop-1][i-1]+" , ");
            tempResult.add(row);
        }
        System.out.println(" - ");
    }

    rs.close();
    st2.close();
    con.close();
    //System.out.println("Closed connection.");
    return tempResult.toArray(new String[0][noOfColumns]);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks very much Paul. Any chance of helping me rewrite this to work better?
Thanks Paul for your help, I tried this and unfortunately getting: Exception java.sql.SQLException: Column index out of range.
I would add that not converting it to an array at all if possible and sticking with a List<> or ArrayList<> instead is prudent.
@James, the exception is probably coming from inside "getResultSwitch", and since you don't provide source, I don't know what's going on there. Try passing in "i+1" as the last argument.
-3

Although I do not know which line of code throws this exception I see at least one problem in your code:

result[loop-1][i-1]

loop-1 is -1 on first iteration because loop = 0

2 Comments

Thanks AlexR for you speedy response! Any suggestions on what it should be?
No it's not. There is a loop++ before the line in question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.