【问题标题】:Update specific Cell from Jtable in SQL从 SQL 中的 Jtable 更新特定单元格
【发布时间】:2018-01-08 19:01:20
【问题描述】:

您好,我正在尝试更新 JTable 中的特定单元格。我正在进行预订,一旦所选预订被批准,它将从“否”更改为“是”。 但是,在单击“批准”后,ReservationStatus 中的所有列都已从“否”更改为“是”,尽管我只希望更改特定的单元格(例如第 0 列第 6 行)。 这是我的 Jtable 代码:

table = new JTable(){
        public boolean isCellEditable(int row, int col) {
            return false;
        };
    };
    table.getTableHeader().setReorderingAllowed(false);
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            row = table.rowAtPoint(e.getPoint());
            col = table.columnAtPoint(e.getPoint());
            data = table.getValueAt(row, col);
            System.out.println("Row= " + row + "column =" + col + "Data= " + data);
            DefaultTableModel model = (DefaultTableModel)table.getModel();
            int selectedRowIndex = table.getSelectedRow();
            if(e.getClickCount() == 2) {
                if(col == 6){
                    frame.setContentPane(new approvalReservation(frame));
                    approvalReservation.lblDisplayAdminNo.setText(model.getValueAt(selectedRowIndex, 0).toString());
                    approvalReservation.lblDisplayMusicalInstrument.setText(model.getValueAt(selectedRowIndex, 1).toString());
                    approvalReservation.lblDisplayDuration.setText(model.getValueAt(selectedRowIndex, 2).toString());
                    approvalReservation.lblDisplayExtension.setText(model.getValueAt(selectedRowIndex, 5).toString());
                }
                else if(col == 7) {
                    frame.setContentPane(new approvalAccess(frame));
                    approvalAccess.lblDisplayAdminNo.setText(model.getValueAt(selectedRowIndex, 0).toString());
                    approvalAccess.lblDisplayLockerNumber.setText(model.getValueAt(selectedRowIndex, 3).toString());
                    approvalAccess.lblDisplayLocation.setText(model.getValueAt(selectedRowIndex, 4).toString());
                }

            }
        }
    });


    scrollPane.setViewportView(table);

    JButton btnLoadTable = new JButton("Load Table");
    btnLoadTable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            approvalController ac = new approvalController();
            ArrayList<String> approvalList = approvalController.displayTableReservation();
            for(int i = 0; i < approvalList.size(); i++) {
                approvalList.get(i);
            }

        }
    });

PS:我将 row 和 col 声明为 public static,因此两者都可以传递给另一个 JPanel。

这是审批表代码:

JButton btnApprove = new JButton("Approve");
    btnApprove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Connection connection=null;
            connection = sqliteConnection.dbConnector();
            try {
                String query = "Update approval set reservationStatus = 'Yes' where reservationStatus = '" + approvalMain.data + "'";
                PreparedStatement pst = connection.prepareStatement(query);
                pst.executeUpdate();
                System.out.println("Updated");
                JOptionPane.showMessageDialog(null, "Data Updated");


            }

            catch(Exception e) {
                e.printStackTrace();
            }
        }
    });
    btnApprove.setBackground(SystemColor.info);
    btnApprove.setBounds(650, 361, 135, 50);
    panel.add(btnApprove);

另一个问题是,当 ReservationStatus 为“否”时,用户无法点击下一列 AccessStatus。因为 AccessStatus 只有在 ReservationStatus 被批准后才能点击,从 No 到 Yes。我该怎么做呢?到目前为止,列内的两个单元格都可以单击。

JTable Before Approving

After clicking on Approve, note that all the cells in ReservationStatus has been changed from No to Yes

【问题讨论】:

  • 1) 您的“批准”按钮甚至不会更新 JTable,所以谁知道表中的值是如何变化的。您的代码所做的只是在您的数据库上调用 SQL。此外,您使用的 PreparedStatement 不正确。您应该为 PreparedStatement 设置参数。例如:stackoverflow.com/questions/15713360/…
  • 2) 为什么要使用 MouseListener?看起来这个逻辑可能会完全刷新 JTable。如果是这样,那么这告诉我您更新数据的 SQL 将所有值设置为“是”。因此,修复 SQL 并正确使用 PreparedStatement,将更容易发现您的错误。不要忘记添加调试代码以显示您传递给 PreparedStatement 的参数值。
  • 3) 我什至不确定您为什么需要 MouseListener 或为什么需要重置内容窗格?编辑表格应该会导致表格自行更新。此外,您将使用setModel(...) 方法来刷新表,而不是创建一个全新的表。
  • @camickr 批准按钮应该更新数据库表中包含“否”字样的单元格,但是它更新了所有列而不是列中的特定行
  • 我建议您的 SQL 错误。这意味着您需要修复“where”子句以仅更新要更新的行。我现在不知道您的数据库的结构,但您需要指定唯一标识每一行的“键”。

标签: java sql swing jtable


【解决方案1】:
String query = "Update approval set reservationStatus = 'Yes' where reservationStatus = '" + approvalMain.data + "'";

假设approvalMain.data 等于'No',此查询表示更新当前状态为'No' 的每一行的状态。如果您只想更改一行,则需要在 where 子句中指定它,最好使用表的主键列。

【讨论】:

  • 是的,approvalMain.data 会给出“否”,但是 row = table.rowAtPoint(e.getPoint()); col = table.columnAtPoint(e.getPoint());数据 = table.getValueAt(row, col);数据不应该获取该 Jtable 的特定行和列的值吗?在这种情况下,第 0 行第 6 列的值为“否”,它不应该更新该特定值而不是所有内容吗?
  • 那么,我该如何更新特定的行和列呢?我试过 table.getModel().getValueAt(approvalMain.col,approvalMain.row) +"'" 但效果不佳
  • 它正在获取特定行的值。然后您的查询告诉数据库更新具有该值的任何行,并且多行具有相同的值。它无法知道哪个相同的值是你给它的,所以它匹配所有的值。您需要一个 where 子句,其值仅与您想要的行匹配。使用数据库表的主键。
  • 好的,我明白了!我需要一个唯一的 ID 来匹配特定的行,对吗?就我而言,我有一个唯一的reservationNo,所以我可以使用reservationNo 选择特定的reservationStatus,对吗? imgur.com/a/S3YYD
  • @Ruiru, Okay i get it ! I need a unique ID to match the specific row right?- 这是我一个多小时前所说的。
猜你喜欢
  • 1970-01-01
  • 2012-03-05
  • 2012-09-24
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多