从 JDBC ResultSet 创建数组时出现 java.lang.ArrayIndexOutOfBoundsException

发布于 2024-10-04 17:45:38 字数 1608 浏览 2 评论 0原文

谁能告诉我我在这里做错了什么?我运行了大约 100 次,大约有 2-8 次失败并出现错误: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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-10-11 17:45:38

我建议您创建一个结果行的 ArrayList,然后将其转换,而不是使用 rs.last() 和 rs.getRow() 来创建静态数组完成后放入数组中。

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

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]);
揪着可爱 2024-10-11 17:45:38

虽然我不知道哪一行代码抛出此异常,但我在代码中至少看到一个问题:

result[loop-1][i-1]

loop-1 在第一次迭代时为 -1,因为loop = 0

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文