总结
如果从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>