Insert data into a table from a Java form (Netbeans+SQL Server)

Asked

Viewed 2,506 times

4

I have a form created so that I can enter data about a client and from this form open a table with a button that has the data, so far I have been able to link the buttons. Now my question is how I can send the form data to the table... I don’t want the code, I just need you to explain to me how I do it.

  • You want to send form data to table or database data?

  • I couldn’t understand your question. When you refer to "table"/"table", it is unclear whether you speak of the swing Jtable or a table in SQL Server.

  • I want to send the form data to the table and after the table to the DB... I use netbeans and sql server 2008

  • Take the data from TextFields and move to a List then load that List in your table and at the end of to create a button to save all data from the list in your database.

  • I’ll try to do that and then put the answer here. Thank you... I’m still learning Java so it’s complicated

  • All right, if you want something more specific just ask here that I try to pass a more complete solution.

  • I understand in theory how I have to do it, but can’t you give me an example and explain? Thank you

  • 1

    @welcome diiana. You have questions: "How to send data from a form to a JTable?" and "How to send data from a JTable to the database?". A suggestion is that you break this question in two, the way it is at the moment the answers would be broad.

  • Thanks :3 but initially I just really want to figure out how to send the form data to Jtable.. @re22

Show 4 more comments

1 answer

2

Jtable has a Table Model... create a class that extends Defaulttablemodel, and assemble your model.. modify the template, then make table.setModel(yourModel)

Example:

for a class with these attributes

class Cliente{
   Integer id;
   String nome;
   //getters and setters
}

You can do the following:

class ClienteTableModel extends DefaultTableModel{
     public ClienteTableModel(){
        this.addColumn("ID");
        this.addColumn("NOME");
     }

     public ClienteTableModel(List<Cliente> listClientes){
        this();
        for(Cliente c: listClientes){
            this.addRow(new String[]{c.getId(),c.getNome()});
        }
     }
}

Then, in your view, you do so:

List<Cliente> listClientes = buscaClientes();
ClienteTableModel model = new ClienteTableModel(listClientes);
table.setModel(model);
  • 2

    How do you do it? You can give an example?

  • 1

    Post an example to make it easier for the questioner and other members to understand.

  • I edited the answer and put an example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.