数组长度0?

bq8i3lrv  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(439)

这个问题在这里已经有答案了

java的preparedstatement是如何工作的(5个答案)
11个月前关门了。
我正在做的java/sql项目中的特定preparedstatement有问题。当我调用“edit”方法(并且只有“edit”方法)时,我得到一个arrayindexoutofboundsexception。更令人费解的是,我的索引值是基于一的,例外情况是数组的长度是0。为什么会发生这种情况,如何修复?
在此行引发异常:

pstmt.setInt(1, newID);

代码:

public void editPatientSQL(String[] info) {
        String sql = "UPDATE Patient SET ID = 'newID', Name = 'name', Phone = 'phone', DoctorID = 'doctorID' WHERE ID = 'oldID'";
        int newID, phone, doctorID, oldID;
        String name;

        newID = Integer.parseInt(info[0]);
        name = info[1];
        phone = Integer.parseInt(info[2]);
        doctorID = Integer.parseInt(info[3]);

        oldID = Integer.parseInt(model.getValueAt(selectedRow, 0).toString());

        try {
            Connection conn = connect();
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1, newID);
            pstmt.setString(2, name);
            pstmt.setInt(3, phone);
            pstmt.setInt(4, doctorID);

            pstmt.executeUpdate();

            pstmt.close();
            conn.close();
        }catch(SQLException ex) {
            System.out.println(ex.getMessage());
        }

    }

例外情况:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:121)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.setInt(JDBC3PreparedStatement.java:324)
    at hospitalDatabase.DatabaseGui.editPatientSQL(DatabaseGui.java:490)
    at hospitalDatabase.DatabaseGui.actionPerformed(DatabaseGui.java:619)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6397)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
kmynzznz

kmynzznz1#

您应该使用问号来指示参数应该插入的位置。

String sql = "UPDATE Patient SET ID = ?, Name = ?, Phone = ?, DoctorID = ? WHERE ID = ?";

相关问题