简体   繁体   中英

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. 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.

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.

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

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.

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.

Google tells me that NetBeans has some facilities for doing this, but I don't know whether then can be externalized outside the IDE.

The time you have to do this is your concern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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