gpt4 book ai didi

java - Jtable - 编辑后获取表格单元格数据

转载 作者:行者123 更新时间:2023-12-01 21:59:28 25 4
gpt4 key购买 nike

我有一个 JTable,其中包含数据库中的数据。我可以使用

提取表中当前的数据
table.getValueAt(table.getSelectedRow(),3)    

我不明白的是如何在运行应用程序中编辑后获取同一单元格的新数据。我尝试运行更新 SQL 命令来推送新数据,但它没有更新。我想我错过了一步。我也尝试过 TableModelListener,但我很难让它按照我需要的方式工作。这是我迄今为止协助检索和更新数据的内容:

private void UpdateDbFromJTable(){
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent event) {
if (table.getSelectedRow() > -1) {
// print first column value from selected row
String App = table.getValueAt(table.getSelectedRow(),0).toString();
String Version = table.getValueAt(table.getSelectedRow(),1).toString();
String Issue = table.getValueAt(table.getSelectedRow(),2).toString();
String Resolved =table.getValueAt(table.getSelectedRow(),3).toString();


try {
String sql = "UPDATE Bugs SET Resolved =" + Resolved + "WHERE App = \" " + App + "' AND Version =\"" + Version + "\" AND Issue = \"" + Issue + "\"";
conn = DriverManager.getConnection(DB_URL);
stmt = conn.prepareStatement(sql);
rs = stmt.executeUpdate();


} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);

}
}

}

});


}

此时 Update 语句不正确,因为它只是将“已解决”更新为数据库中已设置的内容。

这就是我修正更新后所发生的情况。更改不同的表条目后,每次单击“保存”时,都会出现重复的“println”: 私有(private)无效 UpdateDbFromJTable(){

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent event) {
String App = null;
String Version = null;
String Issue = null;
String Resolved = null;
if (! event.getValueIsAdjusting())
{
if (table.getSelectedRow() > -1) {
// print first column value from selected row
App = table.getValueAt(table.getSelectedRow(),0).toString();
Version = table.getValueAt(table.getSelectedRow(),1).toString();
Issue = table.getValueAt(table.getSelectedRow(),2).toString();
Resolved =table.getValueAt(table.getSelectedRow(),3).toString();

System.out.println(App + " " + Version + " " + Issue + " " + Resolved);
}

}
if(table.isEditing()){
table.getCellEditor().stopCellEditing();

try {


conn = DriverManager.getConnection(DB_URL);
String sql = "UPDATE Bugs SET Resolved = ? WHERE App = ? AND Version = ? AND Issue = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, Resolved);
stmt.setString(2, App);
stmt.setString(3, Version);
stmt.setString(4, Issue);
stmt.executeUpdate();





} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);

}
}

}

});


}

我的保存按钮:

btnSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
UpdateDbFromJTable();
JTable();

}
});

和我的 JTable():

Connection conn = null;
ResultSet rs = null;
PreparedStatement stmt = null;
private JTable table;
private void JTable() {
String sql = "SELECT * FROM Bugs";
try {
conn = DriverManager.getConnection(DB_URL);
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));



} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);

}


}

最佳答案

您可以尝试添加TableModelListener这种方式,一旦表发生更改就会执行。像这样的东西:

tableModel.addTableModelListener(new TableModelListener(){
@Override
public void tableChanged(TableModelEvent tableModelEvent) {
if(table.isEditing())
String value = table.getValueAt(table.getSelectedRow(),3);
//do stuff with value
}
});

关于java - Jtable - 编辑后获取表格单元格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33811113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com