如何将删除的行保存到数据库中?

How to save the deleted row into database?

对不起大家,我重新post我的问题。问题是当我按下删除按钮时,jtable 中删除的行消失了,但是当我重新打开 jtable 时,这个删除的行并没有删除。我认为问题是删除这一行时, 此操作未保存到 sqlite 中。请帮助我如何实施和解决我的问题。 我用MVC实现CRUD,数据库我用sqlite。另外,我还有一个问题?如何实现当我按下编辑按钮时,Jtable数据将被修改并保存到数据库中。

我的代码在下面

//Model
public class ClientModel extends AbstractTableModel {

    private List<Client> listClient = new ArrayList<Client>();

    **// delete data**
    public void removeSelectedFromTable(JTable clientTable) {
        int[] rows = clientTable.getSelectedRows();
        TableModel tm = (DefaultTableModel) clientTable.getModel();
        for (int row : rows) {
            ((DefaultTableModel) tm).removeRow(clientTable.convertRowIndexToModel(row));
        }
        clientTable.setModel(tm);
    }

演示

package demo;
@SuppressWarnings("serial")
public class SileaiFrame extends JFrame {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SileaiFrame Jframe = new SileaiFrame();
                    Jframe.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.print(e);
                }
            }
        });
    }

    /**
     * Create the frame.
     * 
     * @throws ParseException
     */
    @SuppressWarnings("rawtypes")

    public SileaiFrame() throws ParseException {
        setTitle("Database Management");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 793, 477);
        exportMenuBar = new JMenuBar();
        setJMenuBar(exportMenuBar);

        clientLayeredPane = new JLayeredPane();
        jftp_0.addTab("Client information", null, clientLayeredPane, null);
        GridBagLayout gbl_clientLayeredPane = new GridBagLayout();
        gbl_clientLayeredPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        gbl_clientLayeredPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
        gbl_clientLayeredPane.columnWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0,
                Double.MIN_VALUE };
        gbl_clientLayeredPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
        clientLayeredPane.setLayout(gbl_clientLayeredPane);
        categoryComboBox = new JComboBox();
        categoryComboBox.setEditable(true);
        categoryComboBox.setModel(new DefaultComboBoxModel(new String[] { "Activity", "Member", "Donation", "Company"}));
        GridBagConstraints gbc_categoryComboBox = new GridBagConstraints();
        gbc_categoryComboBox.fill = GridBagConstraints.HORIZONTAL;
        gbc_categoryComboBox.insets = new Insets(0, 0, 5, 5);
        gbc_categoryComboBox.gridx = 1;
        gbc_categoryComboBox.gridy = 0;
        clientLayeredPane.add(categoryComboBox, gbc_categoryComboBox);

        organizationTextField = new JTextField();
        GridBagConstraints gbc_organizationTextField = new GridBagConstraints();
        gbc_organizationTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_organizationTextField.insets = new Insets(0, 0, 5, 5);
        gbc_organizationTextField.gridx = 3;
        gbc_organizationTextField.gridy = 0;
        clientLayeredPane.add(organizationTextField, gbc_organizationTextField);
        organizationTextField.setColumns(10);

        nameTextField = new JTextField();
        GridBagConstraints gbc_nameTextField = new GridBagConstraints();
        gbc_nameTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_nameTextField.insets = new Insets(0, 0, 5, 5);
        gbc_nameTextField.gridx = 5;
        gbc_nameTextField.gridy = 0;
        clientLayeredPane.add(nameTextField, gbc_nameTextField);
        nameTextField.setColumns(10);

        noTextField = new JTextField();
        GridBagConstraints gbc_noTextField = new GridBagConstraints();
        gbc_noTextField.insets = new Insets(0, 0, 5, 5);
        gbc_noTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_noTextField.gridx = 7;
        gbc_noTextField.gridy = 0;
        clientLayeredPane.add(noTextField, gbc_noTextField);
        noTextField.setColumns(10);

        zipTextField = new JTextField();
        GridBagConstraints gbc_zipTextField = new GridBagConstraints();
        gbc_zipTextField.insets = new Insets(0, 0, 5, 5);
        gbc_zipTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_zipTextField.gridx = 1;
        gbc_zipTextField.gridy = 1;
        clientLayeredPane.add(zipTextField, gbc_zipTextField);
        zipTextField.setColumns(10);

        addressTextField = new JTextField();
        GridBagConstraints gbc_addressTextField = new GridBagConstraints();
        gbc_addressTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_addressTextField.gridwidth = 2;
        gbc_addressTextField.insets = new Insets(0, 0, 5, 5);
        gbc_addressTextField.gridx = 3;
        gbc_addressTextField.gridy = 1;
        clientLayeredPane.add(addressTextField, gbc_addressTextField);
        addressTextField.setColumns(10);

        cellnumberTextField = new JTextField();
        cellnumberTextField.setColumns(10);
        GridBagConstraints gbc_cellnumberTextField = new GridBagConstraints();
        gbc_cellnumberTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_cellnumberTextField.anchor = GridBagConstraints.WEST;
        gbc_cellnumberTextField.insets = new Insets(0, 0, 5, 5);
        gbc_cellnumberTextField.gridx = 1;
        gbc_cellnumberTextField.gridy = 2;
        clientLayeredPane.add(cellnumberTextField, gbc_cellnumberTextField);

        faxLabel = new JLabel("fax");
        GridBagConstraints gbc_faxLabel = new GridBagConstraints();
        gbc_faxLabel.insets = new Insets(0, 0, 5, 5);
        gbc_faxLabel.gridx = 2;
        gbc_faxLabel.gridy = 2;
        clientLayeredPane.add(faxLabel, gbc_faxLabel);

        faxTextField = new JTextField();
        GridBagConstraints gbc_faxTextField = new GridBagConstraints();
        gbc_faxTextField.insets = new Insets(0, 0, 5, 5);
        gbc_faxTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_faxTextField.gridx = 3;
        gbc_faxTextField.gridy = 2;
        clientLayeredPane.add(faxTextField, gbc_faxTextField);
        faxTextField.setColumns(10);

        emailLabel = new JLabel("email");
        GridBagConstraints gbc_emailLabel = new GridBagConstraints();
        gbc_emailLabel.insets = new Insets(0, 0, 5, 5);
        gbc_emailLabel.gridx = 4;
        gbc_emailLabel.gridy = 2;
        clientLayeredPane.add(emailLabel, gbc_emailLabel);

        emailTextField = new JTextField();
        GridBagConstraints gbc_emailTextField = new GridBagConstraints();
        gbc_emailTextField.insets = new Insets(0, 0, 5, 5);
        gbc_emailTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_emailTextField.gridx = 5;
        gbc_emailTextField.gridy = 2;
        clientLayeredPane.add(emailTextField, gbc_emailTextField);
        emailTextField.setColumns(10);

        JDateChooser birthdayDateChooser = new JDateChooser();
        birthdayDateChooser.setDateFormatString("yyyy-MM-dd");
        GridBagConstraints gbc_birthdayDateChooser = new GridBagConstraints();
        gbc_birthdayDateChooser.fill = GridBagConstraints.HORIZONTAL;
        birthdayDateChooser.setBounds(20, 20, 200, 20);
        gbc_birthdayDateChooser.insets = new Insets(0, 0, 5, 5);
        gbc_birthdayDateChooser.gridx = 1;
        gbc_birthdayDateChooser.gridy = 3;
        clientLayeredPane.add(birthdayDateChooser, gbc_birthdayDateChooser);

        label = new JLabel("~");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.insets = new Insets(0, 0, 5, 5);
        gbc_label.gridx = 2;
        gbc_label.gridy = 3;
        clientLayeredPane.add(label, gbc_label);

        search1DateChooser = new JDateChooser();
        GridBagConstraints gbc_search1DateChooser = new GridBagConstraints();
        gbc_search1DateChooser.fill = GridBagConstraints.HORIZONTAL;
        gbc_search1DateChooser.insets = new Insets(0, 0, 5, 5);
        gbc_search1DateChooser.gridx = 3;
        gbc_search1DateChooser.gridy = 3;
        clientLayeredPane.add(search1DateChooser, gbc_search1DateChooser);

        JButton createButton = new JButton("add");
        GridBagConstraints gbc_createButton = new GridBagConstraints();
        gbc_createButton.insets = new Insets(0, 0, 5, 5);
        gbc_createButton.gridx = 4;
        gbc_createButton.gridy = 4;
        clientLayeredPane.add(createButton, gbc_createButton);
        readButton = new JButton("search");
        GridBagConstraints gbc_readButton = new GridBagConstraints();
        gbc_readButton.insets = new Insets(0, 0, 5, 5);
        gbc_readButton.gridx = 5;
        gbc_readButton.gridy = 4;
        clientLayeredPane.add(readButton, gbc_readButton);

        deleteButton = new JButton("delete");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cm.removeSelectedFromTable(clientTable);
                categoryComboBox.setSelectedItem("");
                organizationTextField.setText("");
                nameTextField.setText("");
                noTextField.setText("");
                zipTextField.setText("");
                addressTextField.setText("");
                cellnumberTextField.setText("");
                faxTextField.setText("");
                emailTextField.setText("");
                birthdayDateChooser.setDate(null);
            }
        });

        GridBagConstraints gbc_deleteButton = new GridBagConstraints();
        gbc_deleteButton.insets = new Insets(0, 0, 5, 5);
        gbc_deleteButton.gridx = 6;
        gbc_deleteButton.gridy = 4;
        clientLayeredPane.add(deleteButton, gbc_deleteButton);

        updateButton = new JButton("modify");
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        });
        GridBagConstraints gbc_updateButton = new GridBagConstraints();
        gbc_updateButton.insets = new Insets(0, 0, 5, 5);
        gbc_updateButton.gridx = 7;
        gbc_updateButton.gridy = 4;
        clientLayeredPane.add(updateButton, gbc_updateButton);

        clientScrollPane = new JScrollPane();
        GridBagConstraints gbc_clientScrollPane = new GridBagConstraints();
        gbc_clientScrollPane.gridwidth = 9;
        gbc_clientScrollPane.fill = GridBagConstraints.BOTH;
        gbc_clientScrollPane.gridx = 0;
        gbc_clientScrollPane.gridy = 5;
        clientLayeredPane.add(clientScrollPane, gbc_clientScrollPane);

        clientTable = new JTable();
        clientTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                categoryComboBox.setSelectedItem(clientTable.getValueAt(clientTable.getSelectedRow(), 0).toString());
                organizationTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 1).toString());
                nameTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 2).toString());
                noTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 3).toString());
                zipTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 4).toString());
                addressTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 5).toString());
                landlineTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 6).toString());
                cellnumberTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 7).toString());
                faxTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 8).toString());
                emailTextField.setText(clientTable.getValueAt(clientTable.getSelectedRow(), 9).toString());
                birthdayDateChooser.setDate((java.util.Date) clientTable.getValueAt(clientTable.getSelectedRow(), 10));
            }
        });

        clientScrollPane.setViewportView(clientTable);
        LoadData();
        activityLayeredPane = new JLayeredPane();
    }

    private void LoadData() {
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("category");
        dtm.addColumn("organization");
        dtm.addColumn("name");
        dtm.addColumn("id");
        dtm.addColumn("zip");
        dtm.addColumn("address");
        dtm.addColumn("cellnumber");
        dtm.addColumn("fax");
        dtm.addColumn("email");
        dtm.addColumn("birthday");
        for (Client client : cm.findAll()) {
            dtm.addRow(new Object[] { client.getCategory(), client.getOrganization(), client.getName(), client.getNo(),
                    client.getZip(), client.getAddress(),  client.getCellnumber(), client.getFax(),
                    client.getEmail(), client.getBirthday() });
        }
        this.clientTable.setModel(dtm);
    }
}

完整代码在这里[http://segmentfault.com/n/1330000003876562]

快速浏览了您提供的代码后,我突然想到我无法找到包含 [=13= 的 StatmentPreparedStatment ] 查询:

PreparedStatement df = ConnectDB.getConn().prepareStatement("DELETE FROM client WHERE organization = ? AND name = ? AND...");
df.execute();

如果您实际上并未在 table 或数据库上执行 DELETE FROM... 查询,则不会删除任何内容。您从模型中删除了项目,但没有从数据库中删除。看起来您需要将 PreparedStatment 添加到 removeSelectedFromTable(JTable clientTable) 以构建并执行 DELETE FROM... 查询。如果您每次重新加载 JTable 时都从数据库重新加载,这将解决您的问题。

警告

请注意,在我上面的示例中,我在 DELETE FROM... PreparedStatment 示例中明确使用了 organizationname

如 MadProgrammer 所述,如果您每次删除行时使用 (row : rows) 从 table 中删除,或基于模型中的索引进行类似操作,则索引将会改变。 这意味着您很可能每次都会在第一行之后删除错误的行集。

我建议您使用 WHERE 子句构建 DELETE FROM...,该子句使用一些其他列,例如名称和组织,这样您就可以增加删除的机会预期的行,并相应地从 table 模型中删除这些行。