简体   繁体   English

使用休眠和Java Swing从MySQL DB恢复表

[英]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. 我如何显示从MySQL恢复的表(在ArrayList中)并使用JTable在Java Swing上显示它?我认为从MySQL恢复表已完成。 My problem is in displaying it on JTable. 我的问题是在JTable上显示它。

Any help will be appreciated. 任何帮助将不胜感激。

Class Afficher.java 类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 . 对于要在Swing JTable显示TBiens表中的记录的部分,您可以看一下下面的Stack Overflow问题:将arrayList数据加载到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. 我不清楚biens列表中是什么类型的对象以及TBiens表在数据库中具有哪些列。

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. 注意:我认为您还将希望使biens列表可用于以下代码,因为现在它仅在btnAfficher按钮的动作侦听器中可见。 When you have solved this, you can call a fillTable method to fill the Swing JTable with the right data: 解决此问题后,可以调用fillTable方法用正确的数据填充Swing JTable

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: fillTable方法可能如下所示:

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 : 它终于可以工作了@Freek de Bruijn :),我编辑了ActionPerformed方法:

           `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. 非常感谢。

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

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