I have connected Java with a database which has worked and now i try to get the data out of the database save it in an arrayliste and visualize it in a Jlist. By trying to do that i get and outofBoundsexception. I am a pretty beginner in that field so if you have mor questions please ask.
My code is here
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;
}
}
And that is my error message:
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)