import java.sql.*;
import javax.sql.*;
import javax.swing.table.AbstractTableModel;
public class RowSetTableModel extends AbstractTableModel
implements RowSetListener {
private RowSet rowSet;
public RowSet getRowSet() {
return rowSet;
}
public void setRowSet(RowSet rowSet) {
if (rowSet != null) {
this.rowSet = rowSet;
rowSet.addRowSetListener(this);
fireTableStructureChanged();
}
}
public int getRowCount() {
try {
rowSet.last();
return rowSet.getRow();
}
catch (Exception ex) {
ex.printStackTrace();
}
return 0;
}
public int getColumnCount() {
try {
if (rowSet != null) {
return rowSet.getMetaData().getColumnCount();
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
return 0;
}
public Object getValueAt(int row, int column) {
try {
rowSet.absolute(row + 1);
return rowSet.getObject(column + 1);
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
return null;
}
public String getColumnName(int column) {
try {
return rowSet.getMetaData().getColumnLabel(column + 1);
}
catch (SQLException ex) {
ex.printStackTrace();
}
return "";
}
public void rowSetChanged(RowSetEvent e) {
System.out.println("RowSet changed");
fireTableStructureChanged();
}
public void rowChanged(RowSetEvent e) {
System.out.println("Row changed");
fireTableDataChanged();
}
public void cursorMoved(RowSetEvent e) {
System.out.println("Cursor moved");
}
}