I'm following this example code to create my own tableModel class to display data from a db using cachedRowSet, and use rowSetEvent to refresh the table when insert a row and delete a row to the cachedRowSet. https://github.com/abissell/jdbctutorial/blob/master/src/com/oracle/tutorial/jdbc/CoffeesTableModel.java
public void rowChanged(RowSetEvent event) {
try {
tm.rs.moveToCurrentRow();
tm = new MyTableModel(tm.rs); // to get right row nums
table.setModel(tm);
} catch (SQLException er) {
er.printStackTrace();
}
}
public class MyTableModel extends AbstractTableModel {
CachedRowSet rs;
ResultSetMetaData meta;
int cols, rows;
public MyTableModel(CachedRowSet r) {
rs = r;
try {
meta = rs.getMetaData();
cols = meta.getColumnCount();
rs.beforeFirst();
rows = 0;
while (rs.next()) {
rows++;
}
rs.beforeFirst();
} catch(SQLException e) {
e.printStackTrace();
}
}
public void insert(String[] info) {
try {
rs.moveToInsertRow();
for(int i = 0;i < cols;i++) {
rs.updateString(i+1, info[i]);
}
rs.insertRow();
rs.moveToCurrentRow();
} catch(SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
try {
rs.beforeFirst();
while(rs.next()) {
if(Integer.parseInt(rs.getString(1))==id) {
rs.deleteRow();
break;
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
@Override
public int getRowCount() {
return rows;
}
@Override
public int getColumnCount() {
return cols;
}
@Override
public String getColumnName(int col) {
try {
return meta.getColumnLabel(col + 1);
} catch (SQLException e) {
return e.toString();
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
if (!rs.absolute(rowIndex + 1)) {
return null;
}
rs.absolute(rowIndex + 1);
Object o = rs.getObject(columnIndex + 1);
if (o == null) {
return null;
} else {
return o.toString();
}
} catch (SQLException e) {
e.printStackTrace();
return e.toString();
}
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
}
con = DriverManager.getConnection(url);
st = con.prepareStatement("SELECT id,name,sex,age,cell FROM PATIENTS");
rs = st.executeQuery();
crs = new CachedRowSetImpl();
crs.setType(ResultSet.TYPE_SCROLL_SENSITIVE);
crs.setConcurrency(ResultSet.CONCUR_UPDATABLE);
crs.populate(rs);
tm is the tableModel.
The table will refresh immediately after a row deleted from the cachedRowSet, but when i insert a new row into cachedRowSet, then the table can't show the newly added row immediately. And i checked the rowChanged() been called and the row inserted inside the db.
Even tried to refetch the table data each time or repaint() still not working. I couldn't figure out why.
Maybe cause swing process racing then it can't refresh immediately? I just opened another JFrame to fill out the data and then insert.
Also, what if i use the resultSet directly inside the model? Will the table automatically updated when you insert a row in the db if i didn't close the connection. Or it's just used to get the initial data and you have to run query again to refresh.
Thank you
=================update==================
I tried to change the table model to use ArrayList to store and update db results, then the insert will be update the table immediately. Maybe cause cachedRowSet update slower?
Confused, don't know why.