-1

This is my tableModel:

public class d9 extends AbstractTableModel {

ArrayList<String> cols = new ArrayList<>();
ArrayList<ArrayList<String>> data = new ArrayList<>();

public d9() {
...
int c = resultSet.getMetaData().getColumnCount();
            while (resultSet.next()) {
            ArrayList<String> eachRow = new ArrayList<>();
            for (int i = 1; i <= c; i++) {
                eachRow.add(resultSet.getString(i));
            }
            data.add(eachRow);
        }
...
}

@Override
public int getRowCount() {
return data.size();
}

@Override
public int getColumnCount() {
return cols.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ArrayList<String> selectedRow = data.get(rowIndex);
return selectedRow.get(columnIndex);
}

@Override
public String getColumnName(int column) {
return cols.get(column);
}

public void removeRow(int rowNumber) {
data.remove(rowNumber);
fireTableRowsDeleted(rowNumber, rowNumber);
}
}

Now, after passing a convertRowIndexToModel line number to removeRow method Row remove from table, But after re-run program, It come back!

Sajad
  • 2,273
  • 11
  • 49
  • 92
  • I assume that this is related to your [previous question](http://stackoverflow.com/questions/17958713/tablemodel-removerow-definition)? Where is the data persisted/stored to? – MadProgrammer Jul 31 '13 at 07:08
  • @MadProgrammer data store to `ArrayList> data...` in the constructor – Sajad Jul 31 '13 at 07:11
  • 1
    But when you re-run the program, where is the data coming from, where is it persisted to? – MadProgrammer Jul 31 '13 at 07:12
  • From what I can guess out of your comment, the data is coming from a database. So did you make sure, its deleted there as well as in your table? Else of course it will always comes back, because you load your data from there and the database never knew that this row was deleted. – crusam Jul 31 '13 at 07:13
  • @MadProgrammer as my table populate own with `data` and `cols` , after re-run the program, That removed selected row, come back to table – Sajad Jul 31 '13 at 07:14
  • @ymene data store from database into `data` ArrayList, when i want to delete a row by selecting it, it remove from table ,but after re-run program, it come back ,i think that it remove from my `data` ! – Sajad Jul 31 '13 at 07:17
  • So, execute the required delete command on the database when you call `removeRow`. There is no linkage between the `ArrayList` and your database, they don't know about each other – MadProgrammer Jul 31 '13 at 07:18
  • @MadProgrammer I add linkage between `ArrayList` and database, But i don't add how connect to database codes to prevent from bustle – Sajad Jul 31 '13 at 07:25
  • @Sajjad there are two ways, both are very similair, difference is in network traffics, you can to start with ResultSetTableModel / TableFromDatabase (reload whole table model, actual snapshot) or to start delete row from database, (if executed, any exception) then to remove from table model, better and easiest is 1st of ways, but network traffic is higher, [for database you could to use Vector (any difference significant database structure and tablemodel structure)](http://stackoverflow.com/a/6901508/714968), no performance issue for util.List in compare with implemented arrays in API – mKorbel Jul 31 '13 at 07:26
  • But the `ArrayList` has no concept of where the data has come from, it is just a container. When you add or remove elements from this `ArrayList`, they don't update the database\, they don't care, that's you job – MadProgrammer Jul 31 '13 at 07:27
  • hey .. don't ask the exact same question again and again: instead learn enough to understand the answers! – kleopatra Jul 31 '13 at 08:06
  • @kleopatra I got answer... – Sajad Jul 31 '13 at 08:11

1 Answers1

3

When you call removeRow you need to try and remove the row from the database.

Now because I have no idea what the structure of your database is, you will need to fill in the details, but this a simple outline of what you need to do

public void removeRow(int rowNumber) throws SQLException {
    Connection con = ...;
    PreparedStatement ps = null;

    String keyValue = ...; // Get key value from the ArrayList

    try {
        ps = con.prepareStatement("DELETE from youDatabaseTabe where key=?");
        ps.setObject(1, keyValue);
        if (ps.executeUpdate() == 1) {
            data.remove(rowNumber);
            fireTableRowsDeleted(rowNumber, rowNumber);
        } else {
            throw new SQLException("Failed to remove row from database");
        }
    } finally {
        try {
            ps.close();
        } catch (Exception e) {
        }
    }
}

You may want to spend some time having a read through JDBC Database Access

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • +1 part of users havent't Java7, then catch is required – mKorbel Jul 31 '13 at 07:27
  • @mKorbel Yeah, haven't made much time to check out auto closure yet :P – MadProgrammer Jul 31 '13 at 07:28
  • I think that there is no need to know the value of each row `ID` number, and we can delete a row by knowing it's row number just. – Sajad Jul 31 '13 at 07:29
  • How? The rows in the database may not be in same order as what you have on the screen? New data may have begin added or removed since you loaded it. There is no way to guarantee that location of the data between the table and the database. That's why we have identifiers in database :P – MadProgrammer Jul 31 '13 at 07:31
  • My records has two column, `ID` and `name` , Do you mean to `Get key value from the ArrayList` is `ID`? – Sajad Jul 31 '13 at 07:32
  • @Sajjad he means nothing, only you need to know which value and where corresponding with value stored in DB – mKorbel Jul 31 '13 at 07:34
  • Yes. How you identify the row is implementation specific, details that you haven't provided, so I just filled it in as best as I could – MadProgrammer Jul 31 '13 at 07:34
  • @MadProgrammer So, Do you mean that i should delete from database and from `ArrayList` too? – Sajad Jul 31 '13 at 07:34
  • Yes. There is no "magical" link between the `ArrayList` and the DB, neither now about each other, that's why you had to populate the `ArrayList` to begin with. The `ArrayList` is nothing more then a container on data. If you want to remove the data from the database, then you must do so. Once you're satisfied, you can then update the model (`ArrayList`) to reflect these changes – MadProgrammer Jul 31 '13 at 07:36
  • @MadProgrammer So nice, i think that when re-run the program, the `ArrayList` updated automatically, correct? – Sajad Jul 31 '13 at 07:42
  • Yes, the `ArrayList` is reloaded from the contents of the database. The `ArrayList` itself is not persistent. – MadProgrammer Jul 31 '13 at 07:44
  • 2
    _There is no "magical" link between the ArrayList and the DB_ yeah, there's no magic in the air _It's your job to synchronize both, no magic involved_ (my comment to the OP's exact same question) - but seemingly you have convinced @Sajjad, finally :-) – kleopatra Jul 31 '13 at 08:12
  • @kleopatra Yeah, Now understand it – Sajad Jul 31 '13 at 08:14
  • @Sajjad Because you've closed the previous connection and haven't re-opened it... – MadProgrammer Jul 31 '13 at 09:05
  • @kleopatra Where do you think I got the comment from ;) – MadProgrammer Jul 31 '13 at 09:05
  • @MadProgrammer my other problem was that i use the old constructor `con` and `resultset` and not create a new of them at `removeRow` method . As my table is defined in another class of tableModel class, I write my `removeRow(int rowNumber , int rowID)` as two parameter to send `rowID` parameter from `table.getValueAt(...)` method. Correct? – Sajad Jul 31 '13 at 15:19
  • That depends. If the ID is not available to the table model (and it should be), then y will need to provide it, as you need some way to identify it – MadProgrammer Jul 31 '13 at 20:05
  • @MadProgrammer Please see this: http://stackoverflow.com/questions/18177249/initialize-variable-with-constructor – Sajad Aug 12 '13 at 21:39