0

我的代码正确地将数据库中的数据显示到表中但是当我想删除一行时,行是从数据库和表模型中删除的,

但是"AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1会出现异常:

public class Model1 extends DefaultTableModel {

public Model1(Vector<Vector<String>> data, Vector<String> column) {
    super(data, column);
}

public void removeRow(int rowToModel, Object rowId) {
    JDBC1 jdbc1 = new JDBC1();

    try {
        Connection con;
        PreparedStatement ps = null;
        con = DriverManager.getConnection(...);

        ps = con.prepareStatement("delete from table where id=?");
        ps.setObject(1, rowId);

        if (ps.executeUpdate() == 1) {
            jdbc1.getColumns().remove(rowToModel);
            fireTableRowsDeleted(rowToModel, rowToModel);
        }

    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}
}

我的 JDBC 类:

public class JDBC1 {

private Vector<String> columns = new Vector<String>();
Vector<Vector<String>> data = new Vector<Vector<String>>();

Connection con;
Statement statement;
ResultSet result;
String query = "Select * from table";

public Vector<String> getColumn() throws SQLException {

    con = DriverManager.getConnection(...);
    statement = con.createStatement();
    result = statement.executeQuery(query);

    int c = result.getMetaData().getColumnCount();
    for (int i = 1; i <= c; i++) {
        getColumns().add(result.getMetaData().getColumnName(i));
    }
    return getColumns();
}

public Vector<Vector<String>> getData() throws SQLException{
    con = DriverManager.getConnection(...);
    statement = con.createStatement();
    result = statement.executeQuery(query);
    int c = result.getMetaData().getColumnCount();

    while (result.next()) {
        Vector<String> newRow = new Vector<String>(c);

        for (int i = 1; i <= c; i++) {
            newRow.add(result.getString(i));
        }
        data.add(newRow);
    }
    return data;

}

public Vector<String> getColumns() {
    return columns;
}

public void setColumns(Vector<String> columns) {
    this.columns = columns;
}
}

图形界面类:

public class GUI1 extends JFrame implements ActionListener {

JTable table;
Model1 model1;
JDBC1 jdbc1;
JButton dellButton;

public GUI1() {
    try {
        jdbc1 = new JDBC1();
        model1 = new Model1(jdbc1.getData(), jdbc1.getColumn());
        table = new JTable(model1);
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    add(new JScrollPane(table), BorderLayout.CENTER);
    add(dellButton() , BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600, 550);
    setLocation(250, 85);
    setVisible(true);
}

public static void main(String[] args) {
    new GUI1();
}

public JPanel dellButton(){
    JPanel panel = new JPanel();
    dellButton = new JButton("Delete Row");
    dellButton.addActionListener(this);
    panel.add(dellButton);
    return panel;
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == dellButton) {
        dellAction();
    }
}

public void dellAction() {
    if (table.getSelectedRow() > -1) {
        int rowToDelete = table.getSelectedRow();
        int rowToModel = table.convertRowIndexToModel(rowToDelete);
        Object rowId = table.getValueAt(table.getSelectedRow(), 0);
        model1.removeRow(rowToModel, rowId);

    } else JOptionPane.showMessageDialog(null, "Select Row");
}
}

输出:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
at java.util.Vector.remove(Vector.java:827)
at test.Model1.removeRow(Model1.java:29)
at test.GUI1.dellAction(GUI1.java:76)
at test.GUI1.actionPerformed(GUI1.java:67)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
4

1 回答 1

2

您似乎将列信息与数据混合在一起。

列描述了在表格中水平显示的列,其中行描述了实际数据(结构化以适合列)......

代替...

jdbc1.getColumns().remove(rowToModel);

尝试...

removeRow(rowToModel);

这将使您不再需要打电话给fireTableRowsDeleted自己...

观察

就个人而言,我会尝试限制该dellAction方法删除行所需的信息量。

我的意思是,该方法已经知道要删除的行,它也不应该查找 ID。这表明该dellAction方法对视图的结构有所了解,将来可能会发生变化。

相反,我个人会从removeRow方法本身的行数据中提取 ID。这使模型有机会根据您的需要对 ID 进行建模或不建模...

恕我直言

于 2013-09-04T21:49:58.607 回答