I have a JTable with data from a database. I can pull the data that is currently in the table using
table.getValueAt(table.getSelectedRow(),3)
What I cannot figure out is how to get the same cell's new data after editing in running App. I've tried running an update SQL command to push the new data but it does not update. I think I am missing a step. I've also attempted TableModelListener but I have had very little luck getting it to work as I need it to. This is what I have so far for assisting the retrieval and update of data:
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);
}
}
}
});
}
The Update statement is incorrect at this point because it is just updating "Resolved" to what it was already set to in the DB.
This is what is happening now that I have the update corrected. I get duplicate 'println'(s) that grows every time I click save after making a change to a different table entry: 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);
}
}
}
});
}
My save Button:
btnSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
UpdateDbFromJTable();
JTable();
}
});
And My 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);
}
}
println
(s) each time I edit an entry in the table theprintln
exponentially grows by two.!e.getValueIsAdjusting
but did not resolve the issue. I appreciate your time you are giving to help with this issue.