我使用 JTable 来显示 MySQL SELECT 查询的结果。这样的表模型代码:
public void setDataSource(ResultSet rs) throws Exception {
data.clear();
columnNames.clear();
columnTypes.clear();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount=rs md .getColumnCount();
for (int i=0; i<columnCount; i++) {
columnNames.add(rsmd.getColumnName(i+1));
Classtype =Cl as s.forName(rsmd.getColumnClassName(i+1));
columnTypes.add(type);
// Here I need to detect is it a joined column
// and if it is to set cell editor for this column to
// a comboBox w/ data from joined table
}
fireTableStructureChanged();
while ( rs.next() ){
ArrayListrow =new ArrayList();
for ( int i=0; i<columnCount; i++) {
if(columnTypes.get(i) == String.class)
row.add(rs.getString(i+1));
else
row.add(rs.getObject(i+1));
}
synchronized(data){
data.add(row);
fireTableRowsInserted(data.size()-1,data .size()-1);
}
}
}
如您所见,某些列可能是通过 JOIN 操作获得的,因此我需要检测实际是什么并将它们的编辑器设置为带有 JOINED 表中可能值的组合框。我认为关于关系数据库和使用它们的良好手册或书籍 w/swing 也很好。谢谢!