我有一个JTable,其中包含数据库中的数据。我可以使用
提取当前表中的数据table.getValueAt(table.getSelectedRow(),3)
我无法弄清楚如何在运行App后编辑后获取相同单元格的新数据。我已经尝试运行更新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语句不正确,因为它只是更新"已解决"到它在DB中已经设置的内容。
现在我正在更新更新。我得到重复的' println'(s),每次我更改到不同的表条目后单击保存时会增长: private void 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);
}
}
答案 0 :(得分:2)
我猜你的编辑器仍处于活动状态,因此数据尚未保存到模型中。
所以你需要在ActionListener中停止编辑:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
查看Table Stop Editing以获取更多信息和其他方法
此外,对您的SQL使用PreparedStatment
它更容易维护并且更不容易出错:
String sql = "UPDATE Page SET Title = ? WHERE Name = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, "update" );
stmt.setString( 2, "Name3" );
stmt.executeUpdate();
stmt.close();
答案 1 :(得分:1)
您可以尝试以表格更改后执行的方式添加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
}
});
答案 2 :(得分:0)
我终于弄明白为什么它会重复,感谢@camickr指出它正在创建多个Listener。我把听众围绕按钮移动了。
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent event) {
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);
}
}
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateDbFromJTable();
//jTable();
}
});
}
});