【问题标题】:ArrayIndexOutOfBoundsException when populating sql results to Jtable将 sql 结果填充到 Jtable 时出现 ArrayIndexOutOfBoundsException
【发布时间】:2015-09-08 04:36:30
【问题描述】:

当我将 SQL 结果填充到 Jtable 时,我使用数组而不是向量。但是我得到了一个著名的错误:当我想访问 JTable (tm.getValueAt(i,12)) 数据时线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException 中出现异常,我不知道是什么导致了这个错误:

public  JTable display () throws ClassNotFoundException, SQLException, ParseException{ 
        java.sql.Connection sqlConnection = getSQLConnection();
        Statement stmt = sqlConnection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT test.CHAINE,plg.Séquence,test.FAMILLE,test.REF,test.OFINTERNE,"
                + "test.CMDCLIENT,test.QTE,test.QTEBAC,test.effectif,plg.RENDEMENT,test.TPSn,"
                + "plg.THA,plg.RendPerte,plg.DateDébut,plg.Délai,plg.DateFin "
                + "from planning plg full outer join TEST_LAMIAA test on test.OFINTERNE=plg.NOF "
                + "where test.termine IS NULL "
                + "ORDER BY  test.chaine,CASE WHEN plg.Séquence Is NULL Then 1 Else 0 End, plg.Séquence");

        DefaultTableModel dtm = new DefaultTableModel() {
        @Override
         public Class<?> getColumnClass(int col) {
       if (col == 1) {
        return Integer.class;
    } else {
        //return getValueAt(0, columnIndex).getClass();
        return String.class;
    }

    }
       public boolean isCellEditable(int rowIndex, int columnIndex) {
        //ici la cellule (1, 2) est non-editable
        if (columnIndex == 0 || columnIndex == 5 || columnIndex == 2 || columnIndex == 15
                || columnIndex == 3 || columnIndex ==10 || columnIndex == 4 || columnIndex == 11
                || columnIndex == 13 || columnIndex == 11) 
            return false;
        //le reste est editable
        return true;
    }

};
       ResultSetMetaData rsmetaData = rs.getMetaData();
        //stores the number of columns
        int colmns = rsmetaData.getColumnCount();
        // the object that will pass data to the jTable 

            while (rs.next())
            {
                Object [] rowData = new Object[colmns];
                for (int i = 0; i < colmns;i++)
                {
                    if (rs.getObject(i+1) != null){
                    rowData[i] = rs.getObject(i+1);
                    }
                }
                dtm.addRow(rowData);
            }
         jTable1.setModel(dtm);

        return jTable1;
   }

public JTable modifyTable (JTable jTable) throws ClassNotFoundException, SQLException, ParseException{

  Object Perte = tm.getValueAt(i,12);
}

【问题讨论】:

  • 能否把错误堆栈跟踪放上以便我们更好地帮助您解决问题

标签: java sql jtable populate


【解决方案1】:

您可能在“rs.getObject(i+1)”处的 if 语句中遇到错误。在这种情况下,即使您正在检查它是否为空,您仍然试图通过访问数组范围之外的数据来执行该检查(为了检查是否为空,您必须首先访问数据。)我不知道确定 Javascript 是否会像某些语言那样自动处理,而另一些则不会。如果没有,那么这可能是您的问题。如果是这种情况,try/catch 会解决这个问题。

【讨论】:

  • 感谢您的回复,我实际上通过使用一个名为 DBUtils 的库解决了这个问题,它让我摆脱了这个问题!
猜你喜欢
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 2014-09-09
  • 2016-04-03
  • 2015-06-19
  • 1970-01-01
相关资源
最近更新 更多