-1

I'm having problems with a specific PreparedStatement in a Java/SQL project I'm doing. When I call the "edit" method (and only the "edit" method), I get an ArrayIndexOutOfBoundsException. Even more bafflingly, my index values are one-based, and the exception is giving the length of the array as 0. Why is this happening, and how could it be fixed?

The exception is thrown at this line:

pstmt.setInt(1, newID);

Code:

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:

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)
  • 2
    You're trying to set parameters for your statement, but the SQL of your statement does not contain any places for parameters (i.e. `?` at the point where your parameter should be inserted). – khelwood Jun 11 '20 at 23:20

1 Answers1

0

You should be using question marks to indicate where parameters should be inserted.

String sql = "UPDATE Patient SET ID = ?, Name = ?, Phone = ?, DoctorID = ? WHERE ID = ?";
Unmitigated
  • 76,500
  • 11
  • 62
  • 80