NetBeans 中的 PreexistingEntityException 错误
我正在尝试使用 Netbeans 8.2 为 uni 项目创建 Java 应用程序。创建用于向数据库添加一些数据的 JFrame
后遇到问题。
NetBeans 突出显示下面代码段中的最终语句 ujc.create(u2);
并显示以下错误消息:
Unreported exception PreexistingEntityException; must be caught or declared to be thrown
有人可以建议我需要做什么来解决此问题吗?我不知道我做错了什么。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
Users u2 = new Users();
u2.setEmployeeno(jTextField1.getText());
u2.setFirstname(jTextField2.getText());
u2.setSurname(jTextField3.getText());
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"BatteryManagementSystemPU");
UsersJpaController ujc = new UsersJpaController(emf);
ujc.create(u2);
}
I am trying to create a Java application using Netbeans 8.2 for a uni project. I'm having an issue after creating a JFrame
for adding some data to the database.
NetBeans highlights the final statement ujc.create(u2);
in the code segment below with the following error message:
Unreported exception PreexistingEntityException; must be caught or declared to be thrown
Can someone advise what I need to do to fix this issue? I have no idea what I'm doing wrong.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
Users u2 = new Users();
u2.setEmployeeno(jTextField1.getText());
u2.setFirstname(jTextField2.getText());
u2.setSurname(jTextField3.getText());
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"BatteryManagementSystemPU");
UsersJpaController ujc = new UsersJpaController(emf);
ujc.create(u2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的 create(...) 方法调用可能会失败,因此我们需要通过使用 try/catch 块包围该方法来允许失败:
您可以在官方网站上找到有关该主题的重要信息教程页面: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
另一种选择是抛出错误,但是,
jButton1ActionPerformed 现在需要捕获错误,因此如果您不确定发生了什么,在大多数情况下最好不要使用此解决方案:
您可以在此处找到有关引发错误的更多信息:https://docs.oracle.com/javase/tutorial/essential/exceptions/投掷.html
The
create(...)
method call here can fail, so we need to allow for that failure by surrounding the method with a try/catch block:You can find great information on the topic at the official tutorial page: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
The other option is to throw the error, however, the calling method for
jButton1ActionPerformed
will now need to catch the error instead, so it's better not to use this solution in most cases if you are unsure on what is happening:You can find more information on throwing an error here: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html