简体   繁体   English

如何将数据库表绑定到JTable并使用JTable来更改和添加行

[英]How do I bind a database table to my JTable and use JTable to change and add rows

In fact I'm using netbeans to develop a fast interface to add and change data to and from a database. 事实上,我正在使用netbeans开发一个快速接口来向数据库添加和更改数据。 The program is simple, it consists of a JTable that is bound to a table in my database. 该程序很简单,它由一个绑定到数据库中的表的JTable组成。 I want something that needs the least amount of code possible. 我想要一些需要尽可能少代码的东西。

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 本文介绍如何将数据绑定到我的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. 问题是我自己没有太多时间这样做,所以如果有办法自动完成,我会很感激。

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 : 首先在您的设计视图中转到框架导航器并展开其他组件节点继续更改usertblList [list]的属性,然后使observable为您要添加到数据库的每个条目创建字段并修改以下代码以适应您的需求 :

    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();

Thereafter proceed to refresh your list 此后继续刷新您的列表

There's no way to do this automatically that I know of. 我知道,没有办法自动完成这项工作。

A tight, wizard-generated binding between UI grids and relational tables is usually available in classic VB or .NET, but not so much in Java. UI网格和关系表之间的向导生成的紧密绑定通常可以在经典的VB或.NET中使用,但在Java中则不是很多。

It simply means that you have to implement Listeners that respond to Swing events and perform the appropriate actions on a relational table using JDBC. 它只是意味着您必须实现响应Swing事件的侦听器,并使用JDBC在关系表上执行适当的操作。

Google tells me that NetBeans has some facilities for doing this, but I don't know whether then can be externalized outside the IDE. 谷歌告诉我 ,NetBeans有一些设施可以做到这一点,但我不知道是否可以在IDE之外进行外部化。

The time you have to do this is your concern. 你必须要做的就是你的担忧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM