简体   繁体   中英

recover table from MySQL DB with hibernate and java Swing

How can I display a table recovered from MySQL (in an ArrayList) and display it on Java Swing using JTable.I think that that recovery of the table from MySQL is done. My problem is in displaying it on JTable.

Any help will be appreciated.

Class Afficher.java

 package com.hibernate.stock;

 import java.awt.BorderLayout;
 import java.awt.EventQueue;

 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.border.EmptyBorder;
 import javax.swing.table.DefaultTableModel;
 import javax.swing.table.TableModel;
 import javax.swing.JButton;

 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import java.util.ArrayList;
 import java.util.List;

 import org.hibernate.SQLQuery;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.Query;

 import javax.swing.JTable;
 import javax.swing.JTextArea;

 public class Afficher extends JFrame {

private JPanel contentPane;
private JTable table;
ArrayList<Object[]> biens= new ArrayList<Object[]>();

/**
 * Launch the application.
 */
private void fillTable(final JTable table, final List biens) {
    final String columnNames[] = {"ID", "Nom", "Catégorie", "Quantité"};
    final DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
    table.setModel(tableModel);
    for (final Object bien : biens) {
        // Assuming each row in the biens list is a list of strings...
        final List<Object> row = (List<Object>) bien;
        tableModel.addRow(row.toArray());
    }
}

     public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Afficher frame = new Afficher();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
     }

     public Afficher() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnAfficher = new JButton("Afficher");
    btnAfficher.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");

    SessionFactory sf = cfg.buildSessionFactory();

    Session s = sf.openSession();

    Transaction tx = s.beginTransaction();
    SQLQuery query=s.createSQLQuery("select * from TBiens");
    biens = (ArrayList) query.list();
    fillTable(table, biens);
    s.flush();
    tx.commit();
    s.close();
        }
    });
    btnAfficher.setBounds(166, 235, 117, 25);
    contentPane.add(btnAfficher);

    JTable table = new JTable();
    fillTable(table, biens);
    // I put the table in a scroll pane and had to make it bigger...
    final JScrollPane tableScrollPane = new JScrollPane(table);
    tableScrollPane.setBounds(224, 90, 400, 500);
    contentPane.add(tableScrollPane);


     }
 }

在此处输入图片说明

For the part where you want to show the records from the TBiens table in the Swing JTable , you can take a look at the following Stack Overflow question: Load arrayList data into JTable .

It is not clear to me what type of objects are in the biens list and which columns the TBiens table has in the database.

Note: I think you will also want to make the biens list accessible for the code below, since it is now only visible in the action listener of the btnAfficher button. When you have solved this, you can call a fillTable method to fill the Swing JTable with the right data:

JTable table = new JTable();
fillTable(table, biens);
// I put the table in a scroll pane and had to make it bigger...
final JScrollPane tableScrollPane = new JScrollPane(table);
tableScrollPane.setBounds(224, 90, 400, 500);
contentPane.add(tableScrollPane);

This fillTable method could look like this:

private void fillTable(final JTable table, final List biens) {
    final String columnNames[] = {"Column A", "Column B", "Column C"};
    final DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
    table.setModel(tableModel);
    for (final Object bien : biens) {
        // Assuming each row in the biens list is a list of strings...
        final List<String> row = (List<String>) bien;
        tableModel.addRow(row.toArray());
    }
}

It finally worked @Freek de Bruijn :), I edited the ActionPerformed method :

           `public void actionPerformed(ActionEvent arg0) {
            try{
            Configuration cfg = new Configuration();
            cfg.configure("hibernate.cfg.xml");

            SessionFactory sf = cfg.buildSessionFactory();

            Session s = sf.openSession();

            Transaction tx = s.beginTransaction();
            SQLQuery query=s.createSQLQuery("select * from TBiens");
            biens = query.list();
            ArrayList<Object[]> res = new ArrayList<Object[]>(biens);

            final String columnNames[] = {"ID", "Nom", "Catégorie", "Quantité"};
            final DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
            table.setModel(tableModel);
            for (final Object[] bien : res) {
                // Assuming each row in the biens list is a list of strings...
                final Object[] row = bien;
                tableModel.addRow(row);

            }

            biens.size();
            System.out.print(i);
            s.flush();
            tx.commit();
            s.close();
            }
            catch (ClassCastException e) {
                e.printStackTrace();
              }



        }
       });`

Thank you so much.

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