1

It's not working. Using the select syntax put table to JTable. Then I want update the changes in JTable to the Database. It's a reasonable idea, if there is an better idea to Update GUI changes to database, please let me know. The following is my code after wondering other people's idea. Now I am only dealing with Updates, Insert/Delete would be harder. Any full tutorial links recommend? I step by step following this link(How to insert, update and delete items from JTable that is loaded from (SQLite) Database)

Edit1: String updatequery = "update emp set department= ? where name = ?"; this one works. But it can only update the department column

Edit2: Now all works fine, but all is based on datatype. When I accidentally mouse touched wrong column. Then there is lots error message.Bacause i use setInt, setString to update table.

public paintSpecificTableCells() throws SQLException {
        TableModel model = initTableModel();
        JTable table = new JTable(model);
        table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
        model.addTableModelListener(new TableModelListener(){
            @Override
            public void tableChanged(TableModelEvent e) {
                int rowFirstIndex = e.getFirstRow();
                int rowLastIndex = e.getLastRow();
                DefaultTableModel model = (DefaultTableModel) e.getSource();

                if(e.getType()==TableModelEvent.UPDATE){
                    int updatedColIndex = e.getColumn();
                    String updateColumn =  table.getColumnName(updatedColIndex);
                    String updatedValue = (String) model.getValueAt(rowFirstIndex, updatedColIndex);
                    String relatedID = (String) model.getValueAt(rowFirstIndex,1);
                    System.out.println("column: "+updateColumn+"  updated value is: "+updatedValue);
                    System.out.println("primary key name is : "+ model.getColumnName(1) +" value: "+relatedID);
                    //column: department  updated value is: Shipping
                    //primary key name is : name value: Dylan
                    String updatequery = "update emp set"+updateColumn+ "="+ "?" +" where name = ?";
                    int affectedrows = 1;

                    try {
                        PreparedStatement pstmt = Main.connection.prepareStatement(updatequery);
                        pstmt.setString(1,updatedValue );
                        pstmt.setString(2, relatedID);
                        affectedrows = pstmt.executeUpdate();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                   // return affectedrows;
                }
            }
        });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jian
  • 4,119
  • 1
  • 17
  • 32
  • *It's not working.* - is NOT a problem description. We don't have access to your application or database. We can't guess what the problem is. If you tell us what is not working MAYBE we can make a suggestion. 1) does your code get updated? 2) did you display the SQL statement? Does is make sense? 3) do you get an SQL error? – camickr Jun 27 '21 at 14:49
  • `String updatequery = "update emp set department= ? where name = ?";` It works. But everytime i test it, the updateColumn string value is` department` So now I can update value on specific column. – jian Jun 27 '21 at 15:51
  • *ut everytime i test it, the updateColumn string value is` department`* - so fix the problem. As I stated we don't have access to your code. The logic that sets your "updatedColumn" variable is not working. Did you display the value of your index. Does it change when you edit a different column? – camickr Jun 27 '21 at 16:02
  • @camickr All problem solved. if possible Please tell me how to insert. Obvious insert data, i will add an button. Can you share some code example or tutorial about insert and delete. Obviously insert the table model changed. So it would be harder. – jian Jun 27 '21 at 16:51
  • Why would it be harder? Instead of an "update" event you will have an "insert" event. Then you get the data from the table and build your SQL. – camickr Jun 27 '21 at 18:44

0 Answers0