gpt4 book ai didi

java - 更新数据库后刷新JTable数据

转载 作者:行者123 更新时间:2023-12-01 04:37:31 30 4
gpt4 key购买 nike

我有一个使用 Netbeans GUI 创建器创建的 JTable,并使用 Vector 对象填充该表。我希望在对数据库进行更改后更新表。我不知道该怎么做。有人可以指导我完成此操作吗?

最佳答案

Try to put this after your update code

rs=st.executeQuery("select * from Table order by columnName");
table.setModel(buildTableModel(rs));



//Create function

private TableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns

Vector<String> columnNames = new Vector<String>();

int columnCount = metaData.getColumnCount();

for (int column = 0; column < columnCount; column++) {

columnNames.add(metaData.getColumnName(column));

}

// data of the table

Vector<Vector<Object>> data = new Vector<Vector<Object>>();

while (rs.next()) {

Vector<Object> vector = new Vector<Object>();

for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {

vector.add(rs.getObject(columnIndex));

}

data.add(vector);

}

return new DefaultTableModel(data, columnNames);
}

关于java - 更新数据库后刷新JTable数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128258/

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