recover table from MySQL DB with hibernate and java Swing

Sarah

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


     }
 }

enter image description here

Freek de Bruijn

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Hibernate returns null for an integer after getting the data from db in Java

분류에서Dev

Recover specific database from `-all-databases` mysql dump

분류에서Dev

Hibernate - get Object version from db

분류에서Dev

RegEx in select from table in db

분류에서Dev

Recover ubuntu from shutdown

분류에서Dev

Java / Hibernate Table does not exist 오류

분류에서Dev

Show fields from MySQL DB from Search

분류에서Dev

Recover Partition Table for exFAT Partition

분류에서Dev

Java Hibernate 다 대일 DB에 저장

분류에서Dev

How to resolve the id from MySQL DB into a variable

분류에서Dev

Replace a string from mySQL Table

분류에서Dev

Get currently logged in user from controller in java swing

분류에서Dev

How to recover from fullscreen crash?

분류에서Dev

Recover files from Damaged Partitiohn

분류에서Dev

Recover systemd from uninterruptible state

분류에서Dev

how to update table(new_DB) from old table(old_DB)

분류에서Dev

Select a specific range from table using hibernate criteria

분류에서Dev

org.hibernate.HibernateException: The database returned no natively generated identity value With db mysql

분류에서Dev

org.hibernate.HibernateException: The database returned no natively generated identity value With db mysql

분류에서Dev

Spring, thymeleaf, JPA/hibernate, mysql UTF-8 characters persist in db

분류에서Dev

db2 query insert from another table

분류에서Dev

Select rows from DB in dependence of field value in child table

분류에서Dev

ManyToMany Java + Hibernate + MySQL의 외래 키

분류에서Dev

Java GUI - NOT Swing

분류에서Dev

mysql contains certain strings from another table

분류에서Dev

MySQL query, sum data from table

분류에서Dev

MySQL Best select query from two table

분류에서Dev

insert multiple rows mysql from another table

분류에서Dev

Selecting MAX number from MySQL table not working

Related 관련 기사

  1. 1

    Hibernate returns null for an integer after getting the data from db in Java

  2. 2

    Recover specific database from `-all-databases` mysql dump

  3. 3

    Hibernate - get Object version from db

  4. 4

    RegEx in select from table in db

  5. 5

    Recover ubuntu from shutdown

  6. 6

    Java / Hibernate Table does not exist 오류

  7. 7

    Show fields from MySQL DB from Search

  8. 8

    Recover Partition Table for exFAT Partition

  9. 9

    Java Hibernate 다 대일 DB에 저장

  10. 10

    How to resolve the id from MySQL DB into a variable

  11. 11

    Replace a string from mySQL Table

  12. 12

    Get currently logged in user from controller in java swing

  13. 13

    How to recover from fullscreen crash?

  14. 14

    Recover files from Damaged Partitiohn

  15. 15

    Recover systemd from uninterruptible state

  16. 16

    how to update table(new_DB) from old table(old_DB)

  17. 17

    Select a specific range from table using hibernate criteria

  18. 18

    org.hibernate.HibernateException: The database returned no natively generated identity value With db mysql

  19. 19

    org.hibernate.HibernateException: The database returned no natively generated identity value With db mysql

  20. 20

    Spring, thymeleaf, JPA/hibernate, mysql UTF-8 characters persist in db

  21. 21

    db2 query insert from another table

  22. 22

    Select rows from DB in dependence of field value in child table

  23. 23

    ManyToMany Java + Hibernate + MySQL의 외래 키

  24. 24

    Java GUI - NOT Swing

  25. 25

    mysql contains certain strings from another table

  26. 26

    MySQL query, sum data from table

  27. 27

    MySQL Best select query from two table

  28. 28

    insert multiple rows mysql from another table

  29. 29

    Selecting MAX number from MySQL table not working

뜨겁다태그

보관