gpt4 book ai didi

java - JTable with/Mysql数据需要JOINS上有jcombobox

转载 作者:行者123 更新时间:2023-11-29 14:13:33 25 4
gpt4 key购买 nike

我使用 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 表中可能值的组合框。我认为关于关系数据库以及使用它们与 swing 的良好手册或书籍也很好。谢谢!

最佳答案

假设您有一个基表(例如 A),与多个其他表连接。您的要求是以不同的方式显示来自连接表的列。

您可以尝试getTableName() ResultSetMetaData 来获取该列所属的表的名称。像这样的事情:

int columnCount = resultSetMetaData.getColumnCount();
System.out.println("Total Columns: "+columnCount);
for(int index = 1; index <= columnCount; index++){
System.out.print("Column Name: "+resultSetMetaData.getColumnName(index));
System.out.println("Table Name: "+resultSetMetaData.getTableName(index));
}

由于您已经知道基表名称,因此您始终可以使用它来比较您正在查看的当前列是否属于基表。

关于java - JTable with/Mysql数据需要JOINS上有jcombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13076100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com