我想使用在另一个 class 处定义的方法(带有查询执行程序)填充一个 jtable
I want to populate a jtable using a method (with a query exec) that is defined at another class
我在 clientRecorderUI JFrame(扩展 JFrame)中有一个 jtable "clientTable"。我还有一个 class 称为 databaseHandler(public class它扩展了我想用作 controller/model(MVC 风格的设计)的 clientRecorderUI)。 databaseHandler 中的方法称为 populateTable(用数据库值填充 table 的 sql)。
我还创建了用于在 clientRecorderUI 中测试相同方法的代码,以查看代码是否合理以及它是否有效(因此 populateTable(); 在评论中)
如何从另一个 class 正确调用方法并利用其图形组件?
顺便说一句,我正在研究 NetBeans。
提前谢谢您!
这就是我在 windowActivated 上调用方法的方式:
private void formWindowActivated(java.awt.event.WindowEvent evt) {
databaseHandler tester=new databaseHandler();
tester.populateTable(); // TODO add your handling code here:
//populateTable();
}
这是 databaseHandler 中的方法:
protected void populateTable(){
String query="Select * from clienttable";
try {
//Call connectDB method to connect to database
Connection dbCon=mySqlConnection.ConnectDB();
//Prepare the query
PreparedStatement pst=dbCon.prepareStatement(query);
//return ResultSet
ResultSet rs=pst.executeQuery(query);
clientTable.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException ex) {
Logger.getLogger(databaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
制作populateTable
return一个TableModel
protected TableModel populateTable(){
TableModel model = null;
String query="Select * from clienttable";
try {
//Call connectDB method to connect to database
Connection dbCon=mySqlConnection.ConnectDB();
//Prepare the query
PreparedStatement pst=dbCon.prepareStatement(query);
//return ResultSet
ResultSet rs=pst.executeQuery(query);
model = DbUtils.resultSetToTableModel(rs);
} catch (SQLException ex) {
Logger.getLogger(databaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return model;
}
然后当你调用它时,只需将它应用到你拥有的 JTable
实例中即可
private void formWindowActivated(java.awt.event.WindowEvent evt) {
databaseHandler tester=new databaseHandler();
TableModel model = tester.populateTable();
// Apply it do what ever JTable you have
}
我在 clientRecorderUI JFrame(扩展 JFrame)中有一个 jtable "clientTable"。我还有一个 class 称为 databaseHandler(public class它扩展了我想用作 controller/model(MVC 风格的设计)的 clientRecorderUI)。 databaseHandler 中的方法称为 populateTable(用数据库值填充 table 的 sql)。
我还创建了用于在 clientRecorderUI 中测试相同方法的代码,以查看代码是否合理以及它是否有效(因此 populateTable(); 在评论中)
如何从另一个 class 正确调用方法并利用其图形组件? 顺便说一句,我正在研究 NetBeans。
提前谢谢您!
这就是我在 windowActivated 上调用方法的方式:
private void formWindowActivated(java.awt.event.WindowEvent evt) {
databaseHandler tester=new databaseHandler();
tester.populateTable(); // TODO add your handling code here:
//populateTable();
}
这是 databaseHandler 中的方法:
protected void populateTable(){
String query="Select * from clienttable";
try {
//Call connectDB method to connect to database
Connection dbCon=mySqlConnection.ConnectDB();
//Prepare the query
PreparedStatement pst=dbCon.prepareStatement(query);
//return ResultSet
ResultSet rs=pst.executeQuery(query);
clientTable.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException ex) {
Logger.getLogger(databaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
制作populateTable
return一个TableModel
protected TableModel populateTable(){
TableModel model = null;
String query="Select * from clienttable";
try {
//Call connectDB method to connect to database
Connection dbCon=mySqlConnection.ConnectDB();
//Prepare the query
PreparedStatement pst=dbCon.prepareStatement(query);
//return ResultSet
ResultSet rs=pst.executeQuery(query);
model = DbUtils.resultSetToTableModel(rs);
} catch (SQLException ex) {
Logger.getLogger(databaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return model;
}
然后当你调用它时,只需将它应用到你拥有的 JTable
实例中即可
private void formWindowActivated(java.awt.event.WindowEvent evt) {
databaseHandler tester=new databaseHandler();
TableModel model = tester.populateTable();
// Apply it do what ever JTable you have
}