前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java中获取Postgresql错误码(getErrorCode/getSQLState)

Java中获取Postgresql错误码(getErrorCode/getSQLState)

作者头像
mingjie
发布2023-05-09 17:55:55
5330
发布2023-05-09 17:55:55
举报
文章被收录于专栏:Postgresql源码分析

总结

如果从Oracle迁移到PG,请把Java中的getErrorCode替换为getSQLState,并适当修改代码逻辑,因为Oracle和PG的错误码没有任何对应关系。

下面表格中列举了一些异常场景,无论执行哪种JAVA中都会抛出SQLException,在异常处理中可以通过三种接口拿到错误信息:

  • getErrorCode
  • getSQLState
  • getMessage

例如:

代码语言:javascript
复制
...
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(e.getErrorCode());
            System.out.println(e.getSQLState());
            System.out.println(e.getMessage());
...

每种场景总结:

异常场景

e.getErrorCode()

e.getSQLState()

e.getMessage()

RAISE division_by_zero;

0

22012

ERROR: division_by_zero

RAISE SQLSTATE ‘20018’;

0

20018

ERROR: 20018

raise exception ‘a’;

0

P0001

ERROR: a

raise exception using errcode=‘20126’, message=‘this message 20126’;

0

20126

ERROR: this message 20126

i := 1 / 0;

0

22012

ERROR: division by zero

与Oracle的区别:

  • PG实现了getSQLState、getMessage两种接口,但getErrorCode永远是0。
  • 而Oracle也实现了getErrorCode接口,可以拿到Oracle特有的负整数错误码。

实例

代码语言:javascript
复制
package org.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:postgresql://1.2.3.4:1234/postgres";
        Connection conn = null;

        try {
            Class.forName("org.postgresql.Driver");

            conn = DriverManager.getConnection(url, "xxx", "xxx");
            System.out.println("Database connected");

            Statement stmt = conn.createStatement();
            stmt.execute(
            """
            create or replace procedure p_err_test1()
            as $$
            DECLARE
              text_var1 text;
              text_var4 text;
            BEGIN
              raise notice 'in p_err_test1';
            
              RAISE division_by_zero;
              -- RAISE SQLSTATE '-20018';
              -- raise exception 'a';
              raise exception using errcode='20126', message='this message 20126';
            -- EXCEPTION WHEN OTHERS THEN
            --   GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT,
            --                           text_var4 = RETURNED_SQLSTATE;
            
            --   raise notice 'MESSAGE_TEXT: %', text_var1;
            --   raise notice 'RETURNED_SQLSTATE: %', text_var4;
            END;
            $$ language plpgsql
            """);
            stmt.execute("call p_err_test1()");


            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(e.getErrorCode());
            System.out.println(e.getSQLState());
            System.out.println(e.getMessage());

        } finally {
            // release resource ....
        }
    }
}

maven

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>pgjdbc17</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.27.jre7</version>
        </dependency>

    </dependencies>
</project>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-05-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 总结
  • 实例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档