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

时间:2015-10-19 06:38:43

标签: 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]

1 个答案:

答案 0 :(得分:1)

在快速查看您提供的代码后,我发现我无法找到包含{StatmentPreparedStatment的{​​{1}} 1}}查询:

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 organization示例中明确使用了nameDELETE FROM...

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

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