开发者

Java setting data to JTable from database error

开发者 https://www.devze.com 2023-03-23 03:43 出处:网络
when i try to show data from my database in table this way: void AddOrderToTable(JTable projectTable) throws SQLException

when i try to show data from my database in table this way:

    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.getStri开发者_如何学JAVAng("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();
}

i get error

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)

where is problem?


Problem is that the table model is trying to display before you've got any data. If the source data isn't loaded yet, you need to handle that condition rather than attempting to access the objects that are still null.


Note that ResultSet columns are numbered from 1, while JTable columns are numbered from 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消