Postgresql/JDBC 使用或不使用 UTF8 编码均失败
Postgresql/JDBC fails with or without UTF8 encoding
使用 Java 8 和 Postgres 10 服务器,以及 v 42.2.16 Postgresql 驱动程序,
我有一个基本的 JDBC 查询功能:
public List<Map<String, Object>> Query(String sql) throws Exception {
Connection con = null ;
PreparedStatement pstmt;
List<Map<String, Object>> resultSetToList = null;
try {
con = DriverManager.getConnection(ConnectionString, Properties);
pstmt = con.prepareStatement( sql );
pstmt.execute();
ResultSet resultSet = pstmt.getResultSet();
resultSetToList = resultSetToList(resultSet);
pstmt.close();
} catch(Exception e){
throw e;
}
finally {
if (con != null)
con.close();
}
return resultSetToList;
}
我执行这样的查询:
Query("SELECT * FROM bi_functions");
但是它抛出异常
org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xa3
数据库(第三方提供)编码SQL_ASCII
。 0xA3 是 'British Pound' 字符。
所以我将查询更改为:
SET LOCAL CLIENT_ENCODING TO 'SQL_ASCII'; SELECT * FROM bi_functions;
它失败了:
org.postgresql.util.PSQLException: The server's client_encoding parameter was changed to SQL_ASCII. The JDBC driver requires client_encoding to be UTF8 for correct operation.
有没有办法使用简单的JDBC来避免这个错误?
Java 在内部使用 Unicode 编码,因此您不能在 JDBC 驱动程序中使用不同于 UTF8
的 client_encoding
。
您应该找出数据库的实际编码(可能是 ISO 8859 或 Windows 编码之一)。然后使用该编码创建一个数据库并转储原始数据库并将转储的数据库加载到其中。
否则您将无法使用此数据库JDBC。
使用 Java 8 和 Postgres 10 服务器,以及 v 42.2.16 Postgresql 驱动程序, 我有一个基本的 JDBC 查询功能:
public List<Map<String, Object>> Query(String sql) throws Exception {
Connection con = null ;
PreparedStatement pstmt;
List<Map<String, Object>> resultSetToList = null;
try {
con = DriverManager.getConnection(ConnectionString, Properties);
pstmt = con.prepareStatement( sql );
pstmt.execute();
ResultSet resultSet = pstmt.getResultSet();
resultSetToList = resultSetToList(resultSet);
pstmt.close();
} catch(Exception e){
throw e;
}
finally {
if (con != null)
con.close();
}
return resultSetToList;
}
我执行这样的查询:
Query("SELECT * FROM bi_functions");
但是它抛出异常
org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xa3
数据库(第三方提供)编码SQL_ASCII
。 0xA3 是 'British Pound' 字符。
所以我将查询更改为:
SET LOCAL CLIENT_ENCODING TO 'SQL_ASCII'; SELECT * FROM bi_functions;
它失败了:
org.postgresql.util.PSQLException: The server's client_encoding parameter was changed to SQL_ASCII. The JDBC driver requires client_encoding to be UTF8 for correct operation.
有没有办法使用简单的JDBC来避免这个错误?
Java 在内部使用 Unicode 编码,因此您不能在 JDBC 驱动程序中使用不同于 UTF8
的 client_encoding
。
您应该找出数据库的实际编码(可能是 ISO 8859 或 Windows 编码之一)。然后使用该编码创建一个数据库并转储原始数据库并将转储的数据库加载到其中。
否则您将无法使用此数据库JDBC。