简体   繁体   English

在JTable单元更改上更新数据库

[英]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. 我有一个窗口,该窗口基于表中的数据显示我的数据,但是麻烦是当我更改并单击保存按钮时,出现错误:错误:未为参数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());
                      }

                    }

                  });

But I do not want to change all columns just the latest 但是我不想只更改所有列的最新内容

Well then you need to change your SQL. 好了,那么您需要更改您的SQL。 Currently your SQL updates all 4 columns. 当前,您的SQL更新所有4列。

If you only want to update a single column, then you need 4 SQL statements. 如果只想更新单个列,则需要4条SQL语句。 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. 我认为您在SQL中还将需要一个“ where”子句。 I don't think you can just specify a row number (but I don't know much about SQL). 我认为您不能只指定行号(但是我对SQL不太了解)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM