使用 Java 中的休眠从数据库填充组合框 [英] Filling combobox from database by using hibernate in Java

查看:24
本文介绍了使用 Java 中的休眠从数据库填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿嘿;

我正在用 java 开发一个带有 hibernate 的小型基于 Swing 的应用程序.我想从数据库列填充组合框.我该怎么做?

I am developing a small swing based application with hibernate in java. And I want fill combobox from database coloumn. How I can do that?

而且我不知道在哪里(在 initComponents, buttonActionPerformd 下)我需要做.

And I don't know in where(under initComponents, buttonActionPerformd) I need to do.

为了保存我正在使用 jbutton,它的代码在这里:

For saving I'am using jbutton and it's code is here :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

 int idd=Integer.parseInt(jTextField1.getText());

 String name=jTextField2.getText();

 String description=jTextField3.getText();

 Session session = null;

 SessionFactory sessionFactory = new Configuration().configure()
    .buildSessionFactory();

 session = sessionFactory.openSession();

 Transaction transaction = session.getTransaction();

   try {


       ContactGroup con = new ContactGroup();

       con.setId(idd);

       con.setGroupName(name);
       con.setGroupDescription(description);



       transaction.begin(); 
       session.save(con); 
       transaction.commit(); 


      } catch (Exception e) {
       e.printStackTrace();
      }

      finally{
       session.close(); 
      }    
}

推荐答案

我不使用 Hibernate,但给定了一个名为 Customer 的 JPA 实体和一个名为 CustomerJpaController 的 JPA 控制器>,你可以做这样的事情.

I don't use Hibernate, but given a JPA entity named Customer and a JPA controller named CustomerJpaController, you can do something like this.

更新:更新代码以反映转换到 EclipseLink (JPA 2.1) 作为持久性库.

Update: Code updated to reflect a switch to EclipseLink (JPA 2.1) as the persistence library.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JFrame;

/**
* @see http://stackoverflow.com/a/2531942/230513
*/
public class CustomerTest implements Runnable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new CustomerTest());
    }

    @Override
    public void run() {
        CustomerJpaController con = new CustomerJpaController(
            Persistence.createEntityManagerFactory("CustomerPU"));
        List<Customer> list = con.findCustomerEntities();
        JComboBox combo = new JComboBox(list.toArray());
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox) e.getSource();
                Customer c = (Customer) cb.getSelectedItem();
                System.out.println(c.getId() + " " + c.getName());
            }
        });
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(combo);
        f.pack();
        f.setVisible(true);
    }
}

对象添加到 JComboBox 从对象的 toString() 方法中获取它们的显示名称,因此 Customer 被修改为返回 getName() for展示用途:

Objects added to a JComboBox get their display name from the object's toString() method, so Customer was modified to return getName() for display purposes:

@Override
public String toString() {
    return getName();
}

您可以了解更多关于JComboBox 文章中的如何使用组合框.

You can learn more about JComboBox in the article How to Use Combo Boxes.

这篇关于使用 Java 中的休眠从数据库填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆