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

标签 java swing sqlite import crud

对不起大家,我重新发布我的问题。问题是当我按下删除按钮时,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]

最佳答案

快速查看您提供的代码后,我发现我无法找到包含以下内容的 StatmentPreparedStatment DELETE FROM...WHERE 查询:

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

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

警告

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

正如 MadProgrammer 所说,如果您使用 (row : rows) 从表中删除,或者每次删除一行时基于模型中索引的类似内容,索引将更改。这意味着您很可能每次都会在第一行之后删除错误的行集。

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

关于java - 如何将删除的行保存到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208062/

相关文章:

Java引用另一个类

c# - C# 的 @string 文字在 Java 中的对应物是什么?

java - 使用缓存和 ArrayAdapter 在 Android 应用程序上进行 JUnit 测试

使用 DefaultModel 不显示 Java JTable 列标题

java - java中通过构造函数实例化对象失败

android - 数据库驱动应用程序中的 OOP 建模对象?

node.js - Node webkit 错误 : please install sqlite3 package manually

javascript - 如何将用户从 servlet 重定向到登录页面?

java - 为什么 Ellipse2D.Double 不被识别?

sql - 在 SQLite 的 SELECT 查询中将整数转换为文本?