通过单击按钮将数据从数据库检索到表 [英] Data retrieving from a database to a table by clicking a button

查看:125
本文介绍了通过单击按钮将数据从数据库检索到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个POS系统。
我输入一个项目编号(在jTextField中),然后按按钮。
然后根据商品编号,其名称和价格应显示在jTable中(与数据库中商品编号相关的记录)

I want to make a POS system. I enter a item number(in jTextField) and press button. Then according to the item number its name and price should display in the jTable(relevant record to the item number in the database)

然后我输入另一个编号然后按下按钮。并且它也应该在jTable中显示而不清除现有数据(作为新行)

Then I enter another number and press button. And it also should display in the jTable without clearing existing data (as a new row)

String SQL = "SELECT name,price FROM items WHERE ID = ' "+jTextField1.getText()+" ' ";
pst = Conn.prepareStatement(sql);
rs = pst.executeQuery();
jTable1.setModel(DbUnits.resultSetToTableModel(rs));

请帮助我构建此文件。
注意:我已经建立了这个程序。我知道这不是一个自由网站!
我需要帮助,以便在不清除现有数据的情况下添加带有数据的新行。

Please help me to build this. Note : I have already built this program. I know this isn't a freelance site ! I'm asking for a help for add new rows with data without clearing existing data.

推荐答案


它还应该显示在jTable中,而不清除现有数据(作为新行)

it also should display in the jTable without clearing existing data (as a new row)

那么您就不能使用 setModel(...)方法,因为这会将所有现有数据替换为新数据。

Well then you can't use the setModel(...) method since that will replace all the existing data with the new data.

而且您不能使用 DbUtils.resultSetToTableModel(...)方法,因为那样会返回新的TableModel。

And you can't use the DbUtils.resultSetToTableModel(...) method since that will return new TableModel.

相反,您需要一次从 ResultSet 读取一行数据。在阅读每一行时,您可以使用 DefaultTableModel addRow(...)方法添加现有模型中的每个新数据行。

Instead you will need to read the data from the ResultSet one row at a time. As you read each row you will then you can use the addRow(...) method of the DefaultTableModel to add each new row of data to the existing model.

这篇关于通过单击按钮将数据从数据库检索到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆