2015-04-24 120 views
-1

我已經將Java連接到已經工作的數據庫,現在我嘗試從數據庫中獲取數據,將它保存在arrayliste中並將其可視化到Jlist中。 通過試圖做到我得到和outofBoundsexception。 我是這個領域的初學者,所以如果你有問題,請提問。Java數據庫連接

我的代碼是在這裏

public ArrayList<Data> getAllSports() throws SQLException { 

    ArrayList<Data> result = new ArrayList<Data>(); 
    String query = "select * from public.sport_type"; 
    Connection connection = connect(); 
    Statement stmt = null; 
    stmt = connection.createStatement(); 
    ResultSet rs = stmt.executeQuery(query); 
    while (rs.next()) { 
     Data sports = new Data(0, query, 0, 0, query, 0, query, query, 0, 
       0, 0); 
     sports.setSportID(rs.getInt("sport_type_id")); 
     model.add(rs.getInt("sport_type_id"), sports); 

     query = "select * from public.match"; 
     stmt = connection.createStatement(); 
     rs = stmt.executeQuery(query); 
     while (rs.next()) { 
      sports.setMatchid(rs.getInt("match_id")); 
      sports.setMatchn(rs.getString("match_description")); 
      model.add(rs.getInt("match_id"), rs.getString("match_description")); 

      query = "select * from public.player"; 

      stmt = connection.createStatement(); 
      rs = stmt.executeQuery(query); 
      while (rs.next()) { 
       sports.setPlayerID(rs.getInt("player_id")); 
       sports.setName(rs.getString("player_name")); 
       sports.setSpeed(rs.getDouble("sprint_speed")); 

       query = "select * from public.measurement"; 



       stmt = connection.createStatement(); 
       rs = stmt.executeQuery(query); 
       while (rs.next()) { 
        sports.setX(rs.getInt("new_x")); 
        sports.setY(rs.getInt("y_zero_up")); 
        sports.setTime(rs.getString("measurement_time")); 
        result.add(sports); 


        System.out.println("run"); 

       } 
      } 
     } 
    } 
    Layout.jList1.setModel(model); 
     connection.close(); 
     return result; 
} 

}

這是我的錯誤信息:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 > 0 
    at java.util.Vector.insertElementAt(Unknown Source) 
    at javax.swing.DefaultListModel.add(Unknown Source) 
    at Database.Database_Connection.getAllSports(Database_Connection.java:85) 
    at visual.Layout.ProcessButton1ActionPerformed(Layout.java:910) 
    at visual.Layout.access$15(Layout.java:905) 
    at visual.Layout$16.actionPerformed(Layout.java:284) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
+0

對於每個子查詢,您必須使用單獨的'ResultSet',你也應該確保你做出嘗試關閉/釋放資源你創造的東西。查看[The try-with-resources Statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)以獲取更多詳細信息 – MadProgrammer

+0

'model.add(rs.getInt(「 sport_type_id「),sports);'是你問題的根源。 'DefaultListModel#add(int,Object)'會嘗試在ListModel中的指定索引處添加指定的Object,但是如果ListModel沒有足夠的元素來覆蓋索引,它會失敗。 – MadProgrammer

+0

非常感謝你我改變了;')但不幸的是,這不是解決方案 –

回答

0

所以兩件事情讓我擔心......

  1. 創建一個ResultSet的新實例,但是將其分配給相同的變量,然後試圖繼續使用它,就好像它沒有改變一樣。每個子查詢必須有它自己的ResultSet
  2. model.add(rs.getInt("sport_type_id"), sports);可能會嘗試將Object添加到DefaultListModel中不存在的位置。你不能添加值VectorArrayList(通常來頭DefaultListModel失序,這是這些數據結構而不是如何工作。

你也應該盡一切努力,關閉您創建的任何資源,例如...

public ArrayList<Data> getAllSports() throws SQLException { 

    ArrayList<Data> result = new ArrayList<Data>(); 
    String query = "select * from public.sport_type"; 
    try (Connection connection = connect()) { 
     Statement stmt = connection.createStatement(); 
     try (ResultSet rs = stmt.executeQuery(query)) { 
      while (rs.next()) { 
       Data sports = new Data(0, query, 0, 0, query, 0, query, query, 0, 
           0, 0); 
       sports.setSportID(rs.getInt("sport_type_id")); 
       model.add(rs.getInt("sport_type_id"), sports); 

       query = "select * from public.match"; 
       stmt = connection.createStatement(); 
       try (ResultSet rs1 = stmt.executeQuery(query)) { 
        while (rs1.next()) { 
         sports.setMatchid(rs1.getInt("match_id")); 
         sports.setMatchn(rs1.getString("match_description")); 
         model.add(rs1.getInt("match_id"), rs1.getString("match_description")); 

         query = "select * from public.player"; 
         stmt = connection.createStatement(); 
         try (ResultSet rs2 = stmt.executeQuery(query)) { 
          while (rs2.next()) { 
           sports.setPlayerID(rs2.getInt("player_id")); 
           sports.setName(rs2.getString("player_name")); 
           sports.setSpeed(rs2.getDouble("sprint_speed")); 

           query = "select * from public.measurement"; 
           stmt = connection.createStatement(); 
           try (ResultSet rs3 = stmt.executeQuery(query)) { 
            while (rs3.next()) { 
             sports.setX(rs3.getInt("new_x")); 
             sports.setY(rs3.getInt("y_zero_up")); 
             sports.setTime(rs3.getString("measurement_time")); 
             result.add(sports); 

             System.out.println("run"); 

            } 
           } 
          } 
         } 
        } 
       } 
      } 

     } 
    } 
    Layout.jList1.setModel(model); 
    return result; 
} 

看看Collections TrailThe try-with-resources Statement更多細節