如何将数据库表绑定到JTable并使用JTable更改和添加行 [英] How do I bind a database table to my JTable and use JTable to change and add rows
问题描述
实际上我正在使用netbeans开发一个快速接口来向数据库添加和更改数据。该程序很简单,它由一个绑定到我数据库中的表的JTable组成。我想要一些需要尽可能少代码的东西。
In fact I'm using netbeans to develop a fast interface to add and change data to and from a database. The program is simple, it consists of a JTable that is bound to a table in my database. I want something that needs the least amount of code possible.
我希望能够修改和添加行。
本文介绍如何将数据绑定到我的JTable: http://blogs.oracle。 com / NetBeansSupport / entry / populating_jtable_from_mysql_database
I want to be able to modify and add rows. this article shows how to bind data to my JTable : http://blogs.oracle.com/NetBeansSupport/entry/populating_jtable_from_mysql_database
第一个问题是当我编辑单元格时,它在数据库中不会改变。
第二个问题是我希望能够添加到表的链接。
然后最后一个问题是我的一些表之间存在关系,我希望在外键的行中有一种组合框将当前表的行链接到另一个表的另一行。
问题是我自己没有太多时间这样做,所以如果有办法自动完成,我会很感激。
The first problem is that when I edit a cell, it doesn't change in the database. The second problem is that I want to be able to add a link to the table. Then the last problem is that there are relations between some of my tables and I want to have a kind of a combobox in the foreign key's rows to link the row of the current table to another row of another Table. The problem Is that I don't have much time to do this myself, so If there is a way to do it automatically I's appreciate it.
推荐答案
首先在您的设计视图中转到框架导航器&展开其他组件节点继续更改usertblList [list]的属性,然后使observable为您要添加到数据库的每个条目创建字段,并修改以下代码以满足您的需求:
Firstly in your Design View go to the frame navigator & expand the other Components Node proceed to change the properties of usertblList[list] and make observable thereafter create fields for each entry you would like to add to the Database and modify the following code to suit your needs :
String email = txfEmail.getText();
String name = txfName.getText();
String surname = txfSName.getText();
Usertbl obj = new Usertbl();
int idNO = 1;
for (Usertbl usertbl : usertblList) {
idNO++;
}
obj.setId(idNO);
obj.setEmail(email);
obj.setName(name);
obj.setSurname(surname);
DatabaseNamePUEntityManager.getTransaction().begin();
DatabaseNamePUEntityManager.persist(obj);
DatabaseNamePUEntityManager.getTransaction().commit();
此后继续刷新您的清单
这篇关于如何将数据库表绑定到JTable并使用JTable更改和添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!