oracle Servlet 捕获唯一约束异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14874416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:26:55  来源:igfitidea点击:

Servlet catch unique constraint exception

javaoracleexceptionservletsunique-constraint

提问by Lingasamy Sakthivel

I'm trying to insert some data into the table named rmas.

我正在尝试将一些数据插入名为rmas.

The table format is

表格格式为

|rcode|rname|vcode|vanme

Here, I set primary key for rcode.

在这里,我为 rcode 设置了主键。

When I'm inserting a record with existing rcode, it displays something like ORA-0000-1 unique constraint Violated..

当我用现有的 rcode插入一条记录时,它会显示类似ORA-0000-1 unique constraint Violated..

If I'm using the following code, it displays the message even in the case of other errors.

如果我使用以下代码,即使出现其他错误,它也会显示消息。

catch(Exception e)
{
 //out.println("rcode already exists");
}

So, I want to catch that primary key exception only and display as "rcode already exist". Is it possible? If yes, then how?

因此,我只想捕获该主键异常并显示为“rcode 已存在”。是否可以?如果是,那么如何?

Thanks in advance

提前致谢

回答by asifsid88

Exceptionis the parent of all the exception. If you have catch(Exception e) { }block written then all the exceptions will fall into this category. You need to find which exception the compiler is returning. Say if your compiler returns this exception SQLIntegrityConstraintViolationExceptionthen the following block would come

Exception是所有异常的父级。如果你catch(Exception e) { }写了块,那么所有的异常都属于这一类。您需要找到编译器返回的异常。假设您的编译器返回此异常,SQLIntegrityConstraintViolationException则将出现以下块

catch(SQLIntegrityConstraintViolationException e) 
{
  // Error message for integrity constraint violation
}
catch(NullPointerException e)
{
  // Null Pointer exception occured.
}
catch(Exception e)
{
 // Other error messages
}

In this way you can have any number of exception blocks. But make sure more specific exception types are first written and then the parent exceptions

通过这种方式,您可以拥有任意数量的异常块。但请确保首先编写更具体的异常类型,然后才是父异常

回答by Fritz

You're catching an Exception, which is the superclass of all exceptions. By catching this you use the Pokémon Style ("Gonna catch 'em all!") which is, in general, a bad practice since you lose the ability to take different courses of action based on the particular exception that was thrown in that block of code.

您正在捕获一个Exception,它是所有异常的超类。通过捕获它,您将使用 Pokémon Style(“Gonna catch 'em all!”),这通常是一种不好的做法,因为您无法根据该块中抛出的特定异常采取不同的行动方案。代码。

Catch only the exception related to the constraint violation to avoid showing the message for every exception.

仅捕获与约束违规相关的异常,以避免为每个异常显示消息。

Why would you like to do this on a servlet escapes me, but I suggest you take a look at the architecture of your solution and provide a layered approach, catching this exceptions in the Persistence tierand returning your own result code, that defines which message should be displayed to the user.

为什么要在 servlet 上执行此操作使我不明白,但我建议您查看解决方案的体系结构并提供分层方法,在持久层中捕获此异常并返回您自己的结果代码,该代码定义了哪些消息应该显示给用户。

Note: I used Result codeand not Error codeto allow returning a code for a successful operation.

注意:我使用结果代码而不是错误代码来允许返回成功操作的代码。

回答by duffymo

I wouldn't have any such code in a servlet. I think it belongs in a class that lives in your persistence tier. Servlets are HTTP listeners; they shouldn't have database code in them.

我不会在 servlet 中有任何这样的代码。我认为它属于一个属于你的持久层的类。Servlet 是 HTTP 侦听器;他们不应该在其中包含数据库代码。

Have your interface-based persistence class catch that exception and handle it appropriately. Write an inteface-based service that uses the persistence class to fulfill the use case. Let the servlet call the service and figure out what to display next based on what happens.

让基于接口的持久性类捕获该异常并对其进行适当处理。编写一个基于接口的服务,该服务使用持久性类来实现用例。让 servlet 调用服务并根据发生的情况确定接下来要显示的内容。

It's called a layered architecture. I'd recommend it highly.

它被称为分层架构。我强烈推荐它。

回答by BaDr Amer

In my case in a spring boot application, I used DataIntegrityViolationExceptionto catch unique constraint like this:

在我的 Spring Boot 应用程序中,我使用DataIntegrityViolationException来捕获这样的唯一约束:

try {
    userRepository.save(user);
    log.debug("Created Information for User: {}", user);
} catch (DataIntegrityViolationException e) {

}

回答by MAF

I would catch in the following manner:

我会用以下方式捕捉:

catch(Exception ex){
      if(ex.getMessage().contains("UNIQUE KEY"))
           return "Error - Unique Key Violation!!";
      else if(ex.getMessage().contains("FOREIGN KEY"))
           return "Error - Foreign Key Violation!!";
      else
          return "Other Error: " + ex.getMessage();
}

Hope that's simple and functional!!

希望简单实用!!