可能是一个新手 Java ......但它就在这里。使用 JPA 调用 Oracle 11g 数据库的 INSERT 存储过程。就将数据获取到数据库表而言,一切正常。
我已将 JPA 代码包装到 Java Swing 应用程序中。我的问题是数据以某种方式提交了两次。我有一个JOptionPane.showMessageDialog
在我提交数据时显示的。那只显示一次。但是当我查询表格时,表格中有两个条目。这是我的代码....
按钮代码
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
AddNewCustomer();
}
});
btnSubmit.setBounds(24, 341, 91, 23);
frmNewCustomer.getContentPane().add(btnSubmit);
动作方法
public void AddNewCustomer() {
Customer.AddCustomer(this.txtFirstName.getText(),
this.txtLastName.getText(), this.txtAddress.getText(),
this.txtCity.getText(), this.txtState.getText(),
this.txtEmail.getText(), this.txtPhone.getText());
JOptionPane.showMessageDialog(frmNewCustomer, "Added new customer",
"Status", 1);
}
protected static void AddCustomer(String sFirstName, String sLastName,
String sAddress, String sCity, String sState, String sEmail,
String sPhone) {
try {
factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query q = em.createNamedQuery("AddCustomer");
q.setParameter("p_PHONE", sPhone);
q.setParameter("p_STATE", sState);
q.setParameter("p_FIRST_NAME", sFirstName);
q.setParameter("p_ADDRESS", sAddress);
q.setParameter("p_EMAIL", sEmail);
q.setParameter("p_CITY", sCity);
q.setParameter("p_LAST_NAME", sLastName);
int r = q.executeUpdate();
em.getTransaction().commit();
List results = q.getResultList();
if (results != null) {
Object id = results.get(0);
if (id != null) {
System.out.println(id.toString());
}
}
em.close();
} catch (Exception e) {
e.printStackTrace();
}
}