Java从数据库错误设置数据到JTable

时间:2011-07-27 19:39:21

标签: java database swing jtable

当我尝试以这种方式在表格中显示数据时:

    void AddOrderToTable(JTable projectTable) throws SQLException
{
    zadanie=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    sql="SELECT * FROM Orders ;";
    dane=zadanie.executeQuery(sql);
    int n=0;
    while (dane.next())
    {
    uzsakNr=dane.getString("Uzsakymo_nr");
    if (uzsakNr != null) {uzsakNr = uzsakNr.trim();}
    priemDat=dane.getString("Priemimo_data");
    if (priemDat != null) {priemDat = priemDat.trim();}
    irengPav=dane.getString("Irenginio_pavadinimas");
    if (irengPav != null) {irengPav = irengPav.trim();}
    model=dane.getString("Modelis");
    if (model != null) {model = model.trim();}
    tip=dane.getString("Tipas");
    if (tip != null) {tip = tip.trim();}
    serial=dane.getString("Serijinis_nr");
    if (serial != null) {serial = serial.trim();}
    priedai=dane.getString("Priedai");
    if (priedai != null) {priedai = priedai.trim();}
    giedAprasy=dane.getString("Gedimo_aprasymas");
    if (giedAprasy != null) {giedAprasy = giedAprasy.trim();}
    status=dane.getString("Statusas");
    if (status != null) {status = status.trim();}
    grazDat=dane.getString("Grazinimo_data");
    if (grazDat != null) {grazDat = grazDat.trim();}
    pastabos=dane.getString("Pastabos");
    if (pastabos != null) {pastabos = pastabos.trim();}
    prieme=dane.getString("Prieme");
    if (prieme != null) {prieme = prieme.trim();}
    clientId=dane.getString("ClientId");
    if (clientId != null) { clientId =  clientId.trim();}


    projectTable.setValueAt(uzsakNr, n, 0);
    projectTable.setValueAt(priemDat, n, 1);
    projectTable.setValueAt(irengPav, n, 2);
    projectTable.setValueAt(model, n, 3);
    projectTable.setValueAt(status, n, 4);
    projectTable.setValueAt(clientId, n, 5);
    n++;
    }
    zadanie.close();
}

我收到错误

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:432)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:276)
at javax.swing.JTable.convertColumnIndexToModel(JTable.java:1812)
at javax.swing.JTable.setValueAt(JTable.java:1926)
at GetFromDb.AddOrderToTable(GetFromDb.java:195)
at Remontas.newItemMenuItem_2_actionPerformed(Remontas.java:754)
at Remontas$NewItemMenuItem_2ActionListener.actionPerformed(Remontas.java:710)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1051)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1092)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

哪里有问题?

2 个答案:

答案 0 :(得分:1)

问题是表模型在您获得任何数据之前正在尝试显示。如果尚未加载源数据,则需要处理该条件,而不是尝试访问仍为空的对象。

答案 1 :(得分:1)

请注意,ResultSet列的编号为1,而JTable列的编号为0。