在 SQL 中从 Jtable 更新特定单元格
Update specific Cell from Jtable in SQL
您好,我正在尝试更新我的 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 为 "No" 时,用户无法单击下一列 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
String query = "Update approval set reservationStatus = 'Yes' where reservationStatus = '" + approvalMain.data + "'";
假设 approvalMain.data
等于 'No'
,此查询表示要更新当前状态为 'No'
的每一行的状态。如果您只想更改一行,则需要在 where
子句中指定它,最好使用 table.
的主键列
您好,我正在尝试更新我的 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 为 "No" 时,用户无法单击下一列 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
String query = "Update approval set reservationStatus = 'Yes' where reservationStatus = '" + approvalMain.data + "'";
假设 approvalMain.data
等于 'No'
,此查询表示要更新当前状态为 'No'
的每一行的状态。如果您只想更改一行,则需要在 where
子句中指定它,最好使用 table.