如何永久更改jtable的值?

44u64gxh  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(128)

嗨,我正面临一个我无法解决的问题。我正在做我的这个java项目,我创建了一个jtable来显示数据库中的值。在其中一个表列中,我将它的所有值都设置为“pending”,还有一个名为dispatchbutton的按钮,当我按下这个按钮时,我想将其中一行的值从“pending”改为“dispatch”,我无法让它正常工作。
当按下dispatchbutton时,它会将其中一行设置为“dispatched”,但同时在jtable中添加该行的精确副本作为新记录。此外,即使其中一行的值现在是“dispatched”,当我重新启动应用程序时,该值现在也会变回“pending”而不是“dispatched”。下面是一些相关的代码,我包括你的信息和测试。一定要告诉我我需要在我的代码中修改什么才能让它工作,非常感谢。
(注意:我没有在数据库中创建“status”列,我只是在jtable中设置“status”的值)

public mainpage() {
        initComponents();
        table_update();
        status_values();
        //status_dispatched();
    }

    Connection con1;
    PreparedStatement pst;

private void DispatchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               

        String type = txttype.getSelectedItem().toString();
        String name = txtname.getText();
        String quantity = txtquantity.getText();

        try {
            //String query = "INSERT INTO `request`(`id`, `itemtype`, `itemname`, `quantity`) VALUES (?, ?, ?, ?)";
            Class.forName("com.mysql.jdbc.Driver");
            con1 = DriverManager.getConnection("jdbc:mysql://localhost/request", "root", "password");
            pst = con1.prepareStatement("insert into request (itemtype,itemname,quantity) values(?,?,?)");
            pst.setString(1, type);
            pst.setString(2, name);
            pst.setString(3, quantity);
            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Item has been dispatched");
            // To update the newly recorded data to the table
            table_update();
            // To ensure that all records are still pending
            status_values();
            // Display dispatched on the row
            status_dispatched();

            // Set the textfields to empty upon button click
            txttype.setSelectedIndex(-1);
            txtname.setText("");
            txtquantity.setText("");
            txttype.requestFocus();

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }               

private void table_update(){
        try {
            int c;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con1 = DriverManager.getConnection("jdbc:mysql://localhost/request", "root", "password");
                pst = con1.prepareStatement("select * from request");
                ResultSet rs = pst.executeQuery();

                ResultSetMetaData rsd = rs.getMetaData();
                c = rsd.getColumnCount();

                DefaultTableModel model = (DefaultTableModel)DispatchTable.getModel();
                model.setRowCount(0);

                while(rs.next()){
                    Vector v2 = new Vector();

                    for(int i=1; i<=c; i++){
                        v2.add(rs.getString("id"));
                        v2.add(rs.getString("itemtype"));
                        v2.add(rs.getString("itemname"));
                        v2.add(rs.getString("quantity"));
                    }           
                    model.addRow(v2); 
                }

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (SQLException ex) {
            Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void status_values(){
        // Set all Status rows to pending
        for(int row = 0; row<DispatchTable.getRowCount(); row++){
            DispatchTable.setValueAt("Pending", row, DispatchTable.getColumn("Status").getModelIndex());
        }
    }

    private void status_dispatched(){
        // Upon the click of the Dispatch button, the status of that row should be dispatched
        String dispatch_row = txtdispatch.getText();
        int convert_dispatch_row = Integer.parseInt(dispatch_row);
        DispatchTable.setValueAt("Dispatched", (convert_dispatch_row)-1, DispatchTable.getColumn("Status").getModelIndex());
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题