[英]Updating a JTable with database query
The program I am working on contains two classes, GUI & DatabaseHelper. 我正在处理的程序包含两个类,GUI和DatabaseHelper。 The table model used is DefaultTableModel.
使用的表模型是DefaultTableModel。
The GUI contains consists of a simple JTable. GUI包含一个简单的JTable。 It is initialised with data from the DatabaseHelper on startup.
它在启动时使用DatabaseHelper中的数据进行初始化。 This works.
这有效。
However, when trying to load new data into the table, it is not quite so straight forward. 但是,当尝试将新数据加载到表中时,它并不是那么直截了当。
My approach thus far has been: 到目前为止,我的方法是:
model = DatabaseHelper.LoadData() // returns a default table model with new data.
tabel = new JTable();
tabel.setModel();
What happens now is that the loaded data is appended onto the already existing JTable. 现在发生的是加载的数据被附加到已经存在的JTable上。
If it is possible I would like to implement a solution using only the default table model. 如果可能,我想仅使用默认表模型来实现解决方案。 Thank you for any suggestions!
谢谢你的任何建议!
EDIT: 编辑:
public void initGUI(){
setJMenuBar(makeMenuBar());
container = new JPanel(new BorderLayout());
model = db.initialiseTable(); // Load initialisation data from the database
table = new JTable();
table.setModel(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
scrollPane = new JScrollPane(table);
container.add(scrollPane, BorderLayout.CENTER);
add(container);
}
Returning a new model from database: 从数据库返回新模型:
public DefaultTableModel loadData(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/version1", "root", "root");
System.out.println("\nDatabase Connection Established.\n");
String query = "SELECT * FROM table WHERE test_number = 2";
stmt = con.createStatement();
rs = stmt.executeQuery( query );
md = rs.getMetaData();
columns = md.getColumnCount();
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <=columns-1; i++){
row.addElement( rs.getObject(i+1) );
}
data.addElement( row );
}
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
columnNames.add(" ");
columnNames.add("Column 1");
columnNames.add("Column 2");
DefaultTableModel model = new DefaultTableModel(data, columnNames);
return model;
}
ActionPerformed, code handles new model returned from the DatabaseHelper ActionPerformed,代码处理从DatabaseHelper返回的新模型
model = new DefaultTableModel();
model = db.loadData();
table = new JTable();
table.setModel(model);
There is probably a bug in DatabaseHelper.LoadData()
. DatabaseHelper.LoadData()
可能存在一个错误。 For some reason, it doesn't create a new model but always returns the same model. 出于某种原因,它不会创建新模型,但始终返回相同的模型。
Create a new model in DatabaseHelper.LoadData()
and it should work. 在
DatabaseHelper.LoadData()
创建一个新模型,它应该工作。
model = DatabaseHelper.LoadData() // returns a default table model with new data.
tabel = new JTable();
tabel.setModel();
there could be most important issue issue that you re_create TableModel
and JTable
, and this new JTable
instance with TableModel
isn't added to the GUI
, don't do that, there no real reason to ... 可能有一个最重要的问题是你重新创建了
TableModel
和JTable
,而这个带有TableModel
新JTable
实例没有添加到GUI
,不这样做,没有真正的理由......
search of ResulsetTableModel
(there you pass only SQL Query
) or to use excelent code Table from Database by @camickr
搜索
ResulsetTableModel
(只传递SQL Query
)或使用数据库中的优秀代码表 @camickr
First, it looks like you've got extra instantiations in the action handler: 首先,看起来你在动作处理程序中有额外的实例化:
model = new DefaultTableModel();
model = db.loadData();
table = new JTable();
table.setModel(model);
The loadData
method instantiates a DefaultTableModel
for you so you can skip the new DefaultTableModel
and you already have a JTable
from your initGui
method so you should just reference that existing Component
. loadData
方法为您实例化DefaultTableModel
,因此您可以跳过new DefaultTableModel
并且您已经从initGui
方法获得了JTable
,因此您应该只引用现有的Component
。
Now, it looks like you are only adding to the columnNames
object. 现在,看起来您只是添加到
columnNames
对象。 Perhaps you need to clear it or re-instantiate it each time through? 也许您需要清除它或每次重新实例化? If
loadData
will be called more than once it would be cleaner to create columnNames
outside of the loadData
method. 如果
loadData
将被多次调用,那么在loadData
方法之外创建columnNames
会更加清晰。 Add " ", "Column 1" and "Column 2" there and only reference it inside loadData
. 在那里添加“”,“列1”和“列2”,只在
loadData
引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.