Java Swing应用程序与Java Persistence Assistant API& MySQL的 [英] Java Swing Application with Java Persistence Assistant API & MySQL

查看:86
本文介绍了Java Swing应用程序与Java Persistence Assistant API& MySQL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MySQL和JPA创建了一个Java Swing应用程序。我能够从

JFrame表单中添加数据。



以下DBM类将用于使用JPA管理数据库连接。



数据库表的实体是员工。

I have created a Java Swing application using MySQL and JPA. I was able to add the data from a
JFrame form.

The following DBM class will use to manage the DB connectivity using JPA.

The entity for the Database Table is as Employee.

    public class DBM {
    
    private static EntityManagerFactory emf = 
          Persistence.createEntityManagerFactory("CompanyWithJPAPU");
    private static EmployeeJpaController employeeJpaController = new
          EmployeeJpaController(getEmf());
    private static EntityManager em = getEmf().createEntityManager();

    /**
     * @return the employeeJpaController
     */
    public static EmployeeJpaController getEmployeeJpaController() {
        return employeeJpaController;
    }

    /**
     * @return the em
     */
    public static EntityManager getEm() {
        return em;
    }

    /**
     * @return the emf
     */
    public static EntityManagerFactory getEmf() {
        return emf;
    }

    /**
     * @param aEmf the emf to set
     */
    public static void setEmf(EntityManagerFactory aEmf) {
        emf = aEmf;
    }
    
}





我已将数据添加到DB表Employee作为以下方法。





I have done the data adding to the DB table Employee as the following method.

private void AddData() throws PreexistingEntityException, Exception{

      Employee emp = new Employee();

      emp.setEmpNo(txtStudNo.getText().trim());
      emp.setEmpName(txtStudName.getText().trim());
      emp.setEmpAddress(txtAddress.getText().trim());

      DBM.getEmployeeJpaController().create(emp);

  }





1.我的实体类是Employee.java,它位于里面在一个名为实体的包中。

2.持久性单元被命名为CompanyWithJPAPU。

3.我的JPA Controller被命名为'EmployeeJpaController'。

4.我的DBM类用于管理数据库连接,代码放在

以上。



现在我想使用JPA从数据库表中查找,更新和删除记录。我不能

弄清楚如何做到这一点。我想要的是根据唯一的

值(即EmpID)找到特定记录。如果你有人请托盘帮助我。



Chiranthaka。



1. My Entity class is the Employee.java and it resides inside in a package called 'Entities'.
2. The Persistence unit is named as 'CompanyWithJPAPU'.
3. My JPA Controller is named as 'EmployeeJpaController'.
4. My DBM class is used to manage the Database connectivity and the code has placed at the
above.

Now I want to find, update and delete records from the database table using JPA. I cannot
figure out how to do that. All I want is to find an specific record according to a unique
value i.e. EmpID. If you someone please tray to help me.

Chiranthaka.

推荐答案

http://www.winstonprakash.com/articles/netbeans/JPA_Add_Update_Delete.html [ ^ ]


这篇关于Java Swing应用程序与Java Persistence Assistant API& MySQL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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