2011-07-27 69 views
0

設置數據的JTable當我嘗試顯示數據從我的數據庫表是這樣的:從Java數據庫錯誤

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) 

在哪裏的問題?

+0

提示#1:創建了一些輔助方法將處理_if不爲空,然後修剪VALUE_代碼 - [DRY](HTTP: //en.wikipedia.org/wiki/Don%27t_repeat_yourself)。提示#2:停止使用[Ponglish](http://en.wikipedia.org/wiki/Ponglish);) – Crozin

回答

1

問題是表模型試圖在獲取任何數據之前顯示。如果源數據尚未加載,則需要處理該情況而不是嘗試訪問仍然爲空的對象。

1

注意ResultSet列從1編號,而JTable列是從編號爲0

+0

我知道,但它沒有任何意義,我沒有使用ResultSet編號列的唯一名稱 –

+0

An [sscce](http ://sscce.org/)可能會提供一些啓示。 – trashgod