什么是PSQLException的MySQL等价物

时间:2022-09-16 14:22:27

I am trying to move my database and web app from PostgreSQL to MySQL, the application is using play 2.3.0.

我正在尝试将我的数据库和Web应用程序从PostgreSQL移动到MySQL,该应用程序正在使用play 2.3.0。

I made the app work with MySQL but I haven't yet made the error management, i'll need your help there:

我让应用程序与MySQL一起工作,但我还没有进行错误管理,我需要你的帮助:

the original code uses PSQLException like this:

原始代码使用PSQLException,如下所示:

import org.postgresql.util.PSQLException

try {
      SQL("DELETE FROM Lab WHERE id = {id}")
        .on("id" -> lab.id)
        .executeUpdate()
      true
    } catch {
      case e: PSQLException
        if e.getMessage.contains("update or delete on table \"lab\" violates foreign key constraint \"conference_organizedby_fkey\" on table \"conference\"") |
           e.getMessage.contains("update or delete on table \"lab\" violates foreign key constraint \"appuser_lab_fkey\" on table \"appuser\"")
        => Logger.warn(e.getServerErrorMessage.getMessage); false
    }

I don't find the MySQL error handler that is the best for this type of error.

我没有找到最适合此类错误的MySQL错误处理程序。

What do you think I should use?

你觉得我应该怎么用?

1 个解决方案

#1


Just catch SQLException, of which PSQLException is an implementation, and stick to its methods - namely getMessage()

只需捕获SQLException,其中PSQLException是一个实现,并坚持其方法 - 即getMessage()

Unless you really need the extra info PSQLException provides, it is better not to know the actual class of the exception, because then you are invisibly binding your handler to the JDBC connection path.

除非您确实需要PSQLException提供的额外信息,否则最好不要知道异常的实际类,因为这样您就无形地将处理程序绑定到JDBC连接路径。

#1


Just catch SQLException, of which PSQLException is an implementation, and stick to its methods - namely getMessage()

只需捕获SQLException,其中PSQLException是一个实现,并坚持其方法 - 即getMessage()

Unless you really need the extra info PSQLException provides, it is better not to know the actual class of the exception, because then you are invisibly binding your handler to the JDBC connection path.

除非您确实需要PSQLException提供的额外信息,否则最好不要知道异常的实际类,因为这样您就无形地将处理程序绑定到JDBC连接路径。