我正在使用 Camick 的 ListTableModel 和 RowTableModel,JTABLE 中没有数据
我正在尝试使用 ListTableModel 并尝试遵循一些在线示例,但仍然发现很难在我的 Jtable 中显示“结果集”数据。我使用模型-视图-控制器方法在 netbeans 中构建 GUI 应用程序。我的所有 GUI 组件都放置在视图中,控制器正在处理任何更新以及该视图的可见性。模型类拥有所有查询并负责使用 JDBC 直接访问数据库。
“Jtable”“searchTable”是在视图类中创建和初始化的,并且由我的控制器通过方法(getSearchTable())访问。
public JTable getSearchTable(){
// view class
return searchTable;
}
我在控制器类中创建了“ListTableModel”,并遵循 Camick 的教程。
private Product_View productView;
private Product_Model productModel;
private Product product;
public Product_Controller(Product_Model model, Product_View view){
this.productModel = model;
this.productView = view;
view.addSaveListener(new SaveListener());
view.addCloseListener(new CloseListener());
view.addSearchSingleListener(new SearchListener());
}
public void displayProductView(){
this.productView.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.productView.setVisible(true);
} class SearchListener implements ActionListener {
public SearchListener() {
}
@Override
public void actionPerformed(ActionEvent ae) {
ListTableModel model = null;
String prodName = productView.getProductName();
JTable tempTable = productView.getSearchTable();
model = productModel.getProduct(prodName, model);
tempTable = new JTable(model);
}
}
最后,方法“getProduct”位于模型类内部,并由我的控制器类调用。
public ListTableModel getProduct(String productName, ListTableModel model){
String query = "SELECT * FROM Products WHERE name='" + productName + "';";
Statement stmt;
try {
stmt = this.conn.createStatement();
stmt.executeQuery (query);
ResultSet rs = stmt.getResultSet();
writeResultSet(rs);
model = ListTableModel.createModelFromResultSet(rs);
rs.close ();
stmt.close ();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return model;
}
Jtable 不返回任何内容,我如何查看我的 ResultSet 数据(我已将“writeResultSet”返回的值打印到控制台,因此查询正常工作?
I am trying to use the ListTableModel and tried following some examples online and still finding it difficult to show 'resultSet' data in my Jtable. Im using the model-view-controller method building a GUI application in netbeans. All of my GUI components are placed in views and the controller is handling any updates as well visibility to this view. The model class has all the queries and is responsible for accessing the database directly using JDBC.
The "Jtable" "searchTable" is created and initialized inside the view class and is being accessed by my controller via a method (getSearchTable()).
public JTable getSearchTable(){
// view class
return searchTable;
}
I have the "ListTableModel" being created inside the controller class and following Camick's tutorial.
private Product_View productView;
private Product_Model productModel;
private Product product;
public Product_Controller(Product_Model model, Product_View view){
this.productModel = model;
this.productView = view;
view.addSaveListener(new SaveListener());
view.addCloseListener(new CloseListener());
view.addSearchSingleListener(new SearchListener());
}
public void displayProductView(){
this.productView.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.productView.setVisible(true);
} class SearchListener implements ActionListener {
public SearchListener() {
}
@Override
public void actionPerformed(ActionEvent ae) {
ListTableModel model = null;
String prodName = productView.getProductName();
JTable tempTable = productView.getSearchTable();
model = productModel.getProduct(prodName, model);
tempTable = new JTable(model);
}
}
Finally the method "getProduct" is inside the model class and being called by my controller class.
public ListTableModel getProduct(String productName, ListTableModel model){
String query = "SELECT * FROM Products WHERE name='" + productName + "';";
Statement stmt;
try {
stmt = this.conn.createStatement();
stmt.executeQuery (query);
ResultSet rs = stmt.getResultSet();
writeResultSet(rs);
model = ListTableModel.createModelFromResultSet(rs);
rs.close ();
stmt.close ();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return model;
}
The Jtable does not return anything, how can i view my ResultSet data (i have printed out the values being returned by "writeResultSet" to the console and so the query is working correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建表不会将表添加到 GUI。在代码中的某个位置,您需要如下代码:
如果您要更新现有表,那么您不会创建新表,您要做的就是:
在重新创建模型之后。
编辑:
要测试您的 SQL,请创建一个简单的 SSCCE,其代码如下:
Creating a table doesn't add the table to the GUI. Somewhere in your code you need code like:
If you are updating an existing table then you don't create a new table all you do is:
after recreating the model.
Edit:
To test your SQL create a simple SSCCE with code something like: