如何通过单击 jbutton 更新所有空列的值,而 table 中的其他数据在 java 中保持不变?
How to update the value of all empty columns with the click of a jbutton, leaving the other data in a table unchanged in java?
我想在我的 MySQlL
数据库中填充 null
列。
我有一个名为 issuebook
的 table,其中有 6 个字段,最后两个字段为空。
图片如下:
我想要这样,当我单击 jbutton
时,空字段将被更新并在每一行的所有 null
列中插入值。
我希望所有行的值都没有变化。只需用新值填充 null
列。
但是当我点击 jbutton
时,每行的值变得相同。
图片如下(当我点击jbutton
):
我的jbutton
代码如下:
btnIssue = new JButton("ISSUE");
btnIssue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateTimeFormatter dfd=DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
LocalDateTime returnvalue = now.plusDays(30);
DefaultTableModel d= (DefaultTableModel) bookRecord.getModel();
try {
for(int i = 0; i < d.getRowCount(); i++) {
id = d.getValueAt(i, 0).toString();
member = d.getValueAt(i, 1).toString();
book = d.getValueAt(i, 2).toString();
rqst = d.getValueAt(i, 3).toString();
pst = con.prepareStatement("update issuebook set ID = ?, MemberName = ?, BookName = ?, RequestTime = ?, IssueDate = ?, ReturnDate = ? where IssueDate is null and ReturnDate is null");
pst.setString(1, id);
pst.setString(2, member);
pst.setString(3, book);
pst.setString(4, rqst);
pst.setString(5, dfd.format(now));
pst.setString(6, dfd.format(returnvalue));
pst.executeUpdate();
}
JOptionPane.showMessageDialog(null, "Book Issued Successfully!");
issueLoad();
d.setRowCount(0);
}
catch(SQLException e1) {
e1.printStackTrace();
}
}
});
如果您不想更新其他字段,很简单 - 只需不设置它们即可。如果它们未包含在 UPDATE
语句中,则不会对其进行修改。
例如
update issuebook set IssueDate = ?, ReturnDate = ? where IssueDate is null and ReturnDate is null
而且,如果您打算将所有行设置为具有相同的日期(现在和现在+30 天),那么您只需执行一次查询即可。您根本不需要获取现有数据。
我已经有很长时间没有在愤怒中完成任何 Java 但我很确定你可以将所有代码简化为:
btnIssue = new JButton("ISSUE");
btnIssue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateTimeFormatter dfd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
LocalDateTime returnvalue = now.plusDays(30);
try {
pst = con.prepareStatement("update issuebook set IssueDate = ?, ReturnDate = ? where IssueDate is null and ReturnDate is null");
pst.setString(1, dfd.format(now));
pst.setString(2, dfd.format(returnvalue));
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Book Issued Successfully!");
issueLoad();
}
catch(SQLException e1) {
e1.printStackTrace();
}
}
});
我想在我的 MySQlL
数据库中填充 null
列。
我有一个名为 issuebook
的 table,其中有 6 个字段,最后两个字段为空。
图片如下:
我想要这样,当我单击 jbutton
时,空字段将被更新并在每一行的所有 null
列中插入值。
我希望所有行的值都没有变化。只需用新值填充 null
列。
但是当我点击 jbutton
时,每行的值变得相同。
图片如下(当我点击jbutton
):
我的jbutton
代码如下:
btnIssue = new JButton("ISSUE");
btnIssue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateTimeFormatter dfd=DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
LocalDateTime returnvalue = now.plusDays(30);
DefaultTableModel d= (DefaultTableModel) bookRecord.getModel();
try {
for(int i = 0; i < d.getRowCount(); i++) {
id = d.getValueAt(i, 0).toString();
member = d.getValueAt(i, 1).toString();
book = d.getValueAt(i, 2).toString();
rqst = d.getValueAt(i, 3).toString();
pst = con.prepareStatement("update issuebook set ID = ?, MemberName = ?, BookName = ?, RequestTime = ?, IssueDate = ?, ReturnDate = ? where IssueDate is null and ReturnDate is null");
pst.setString(1, id);
pst.setString(2, member);
pst.setString(3, book);
pst.setString(4, rqst);
pst.setString(5, dfd.format(now));
pst.setString(6, dfd.format(returnvalue));
pst.executeUpdate();
}
JOptionPane.showMessageDialog(null, "Book Issued Successfully!");
issueLoad();
d.setRowCount(0);
}
catch(SQLException e1) {
e1.printStackTrace();
}
}
});
如果您不想更新其他字段,很简单 - 只需不设置它们即可。如果它们未包含在 UPDATE
语句中,则不会对其进行修改。
例如
update issuebook set IssueDate = ?, ReturnDate = ? where IssueDate is null and ReturnDate is null
而且,如果您打算将所有行设置为具有相同的日期(现在和现在+30 天),那么您只需执行一次查询即可。您根本不需要获取现有数据。
我已经有很长时间没有在愤怒中完成任何 Java 但我很确定你可以将所有代码简化为:
btnIssue = new JButton("ISSUE");
btnIssue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateTimeFormatter dfd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
LocalDateTime returnvalue = now.plusDays(30);
try {
pst = con.prepareStatement("update issuebook set IssueDate = ?, ReturnDate = ? where IssueDate is null and ReturnDate is null");
pst.setString(1, dfd.format(now));
pst.setString(2, dfd.format(returnvalue));
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Book Issued Successfully!");
issueLoad();
}
catch(SQLException e1) {
e1.printStackTrace();
}
}
});