NetBeans 中的 PreexistingEntityException 错误

发布于 2025-01-13 18:38:47 字数 797 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2025-01-20 18:38:47

这里的 create(...) 方法调用可能会失败,因此我们需要通过使用 try/catch 块包围该方法来允许失败:

try
{
    ujc.create(u2);
}
catch(PreexistingEntityException e)
{
    System.err.println("Caught PreexistingEntityException: " + e.getMessage());
}

您可以在官方网站上找到有关该主题的重要信息教程页面: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

另一种选择是抛出错误,但是,jButton1ActionPerformed 现在需要捕获错误,因此如果您不确定发生了什么,在大多数情况下最好不要使用此解决方案:

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

您可以在此处找到有关引发错误的更多信息: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:

try
{
    ujc.create(u2);
}
catch(PreexistingEntityException e)
{
    System.err.println("Caught PreexistingEntityException: " + e.getMessage());
}

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:

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

You can find more information on throwing an error here: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文