Recay
Recay

Reputation: 23

update database on JTable cell change

I have a window that displays my data based on data in a table, but the trouble is that when I change and I click on the save button I get an error : Error:No value specified for parameter 1.

But I do not want to change all columns just the latest

enregistrer.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) 
                {
                    int row = table.getSelectedRow();
                    int col = table.getSelectedColumn();
                    String sql = null;
                      try {
                        if(col==4)
                            sql = "UPDATE impaye" + "SET Impayé = ?" + "WHERE ID = ? "+ row;


                            preStat =(PreparedStatement) connexion.prepareStatement(sql);
                            preStat.setObject(1, table.getValueAt(row, col));
                            preStat.executeUpdate();
                                preStat.close();

                      } catch (SQLException insertException) {
                           System.out.println("Error:"+insertException.getMessage());
                      }

                    }

                  });

Upvotes: 0

Views: 1753

Answers (1)

camickr
camickr

Reputation: 324098

But I do not want to change all columns just the latest

Well then you need to change your SQL. Currently your SQL updates all 4 columns.

If you only want to update a single column, then you need 4 SQL statements. The statement you use will be based on the index of the column that was changed.

Something like:

String sql = null;

if (col == 0)
    sql = "UPDATE impaye SET Date = ? " + row;
else if (col == 1)
    sql = "UPDATE impaye SET Débiteur = ? " + row);
else if
    ...

preStat =(PreparedStatement) connexion.prepareStatement(sql);
preStat.setObject(1, table.getValueAt(row, col));
preStat.executeUpdate();

I think you will also need a "where" clause in your SQL. I don't think you can just specify a row number (but I don't know much about SQL).

Upvotes: 1

Related Questions