【问题标题】:Java Database Connection [duplicate]Java 数据库连接 [重复]
【发布时间】:2015-07-02 10:18:53
【问题描述】:

我已将 Java 与已运行的数据库连接,现在我尝试从数据库中取出数据,将其保存在 arrayliste 中并在 Jlist 中可视化。 通过尝试这样做,我得到了并且超出了Boundsexception。 我是该领域的初学者,所以如果您有任何问题,请提出。

我的代码在这里

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)

【问题讨论】:

  • 对于每个子查询,您必须使用单独的ResultSet。您还应该确保尝试关闭/释放您创建的资源。更多详情请关注The try-with-resources Statement
  • model.add(rs.getInt("sport_type_id"), sports); 是您问题的根本原因。 DefaultListModel#add(int, Object) 会尝试在ListModel 的指定索引处添加指定的Object,但如果ListModel 中没有足够的元素来覆盖索引,它将失败。
  • 非常感谢我改变了它;') 但不幸的是这不是解决方案

标签: java database postgresql postgis


【解决方案1】:

所以有两件事让我担心......

  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 了解更多详情

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 2016-08-14
    • 2016-01-23
    • 1970-01-01
    相关资源
    最近更新 更多