When I populate a jTable from a CachedRowSet using the DefaultTableModel, the jtable column's title wasn't updated. So I did it manually using the jtable property. Is there any alternative way to get the title name automatically from the database?
This the client
public studentdetails()
{
initComponents();
try {
Interface b = (Interface) Naming.lookup("rmi://localhost:1099/Server");
DefaultTableModel tblModel =
(DefaultTableModel)studentdetailstable.getModel();
tblModel.setRowCount(0);
CachedRowSet studentdetails=b.getArray();
while(studentdetails.next())
{
Vector t = new Vector();
for(int i = 0; i <= studentdetails.getMaxRows(); i++)
{
t.add(studentdetails.getInt("id"));
t.add(studentdetails.getString("names"));
}
tblModel.addRow(t);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
Implementation
public CachedRowSet getArray() throws RemoteException {
try (Connection con = ConnectionProvider.getConnection();
Statement st = con.createStatement();
// ResultSet rs = st.executeQuery("select * from studentnames"))
ResultSet rs = st.executeQuery("select id, names from students")) {
RowSetFactory factory = RowSetProvider.newFactory();
CachedRowSet details= factory.createCachedRowSet();
fruitDetails.populate(rs);
return details;
}
catch (SQLException e) {
throw new RemoteException("Method 'getArray()' failed.", e);
}
CachedRowSet
inherits method getMetaData
which returns ResultSetMetaData which can be used to get the names of the columns as well as the column count. (See below code. More explanation after the code.)
/* Required imports.
import java.rmi.Naming;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.sql.rowset.CachedRowSet;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
*/
public studentdetails() {
initComponents();
try {
Interface b = (Interface) Naming.lookup("rmi://localhost:1099/Server");
CachedRowSet details = b.getArray();
ResultSetMetaData rsmd = details.getMetaData();
int columnCount = rsmd.getColumnCount();
Vector<Object> columnNames = new Vector<>(columnCount);
for (int i = 0; i < columnCount; i++) {
columnNames.add(rsmd.getColumnLabel(i));
}
Vector<Object> rows = new Vector<>();
while (details.next()) {
Vector<Object> row = new Vector<>(columnCount);
for (int i = 0; i <= columnCount; i++) {
row.add(details.getObject(i));
}
rows.add(row);
}
DefaultTableModel tblModel = new DefaultTableModel(rows, columnNames);
studentdetailstable.setModel(tblModel);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}
}
DefaultTableModel
contains a Vector
whose elements are Vector
s. Each element is a row in the JTable
. Hence, in the above code, I create and populate a Vector
that can serve as one of the parameters to the DefaultTableModel
constructor. The other parameter to the DefaultTableModel
constructor is a Vector
containing the names of the columns in the JTable
. As I already said, the column names can be obtained from the ResultsetMetaData
.
After creating a new DefaultTableModel
, I simply assign it to the JTable
via method setModel
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.