Inserting a resultset into jtable directly
If is there any way to insert a resultset into jtable directly?开发者_开发技巧
Bad idea.
You shouldn't be passing anything from the java.sql
package out of your persistence tier.
You can certainly iterate over a ResultSet
and load the contents into your DefaultTableModel
. But I wouldn't recommend it.
Something like this:
public DefaultTableModel map(ResultSet resultSet) throws SQLException
{
DefaultTableModel defaultTableModel = new DefaultTableModel();
ResultSetMetaData meta = resultSet.getMetaData();
int numberOfColumns = meta.getColumnCount();
while (resultSet.next())
{
Object [] rowData = new Object[numberOfColumns];
for (int i = 0; i < rowData.length; ++i)
{
rowData[i] = resultSet.getObject(i+1);
}
defaultTableModel.addRow(rowData);
}
return defaultTableModel;
}
精彩评论