Postgresql/JDBC无论是否使用UTF8编码都失败。

huangapple go评论94阅读模式
英文:

Postgresql/JDBC fails with or without UTF8 encoding

问题

使用Java 8和Postgres 10服务器,以及v 42.2.16的Postgresql驱动程序,我有一个基本的JDBC查询函数:

  1. public List<Map<String, Object>> Query(String sql) throws Exception {
  2. Connection con = null;
  3. PreparedStatement pstmt;
  4. List<Map<String, Object>> resultSetToList = null;
  5. try {
  6. con = DriverManager.getConnection(ConnectionString, Properties);
  7. pstmt = con.prepareStatement(sql);
  8. pstmt.execute();
  9. ResultSet resultSet = pstmt.getResultSet();
  10. resultSetToList = resultSetToList(resultSet);
  11. pstmt.close();
  12. } catch (Exception e) {
  13. throw e;
  14. } finally {
  15. if (con != null)
  16. con.close();
  17. }
  18. return resultSetToList;
  19. }

我执行类似这样的查询:

  1. Query("SELECT * FROM bi_functions");

但是它抛出了一个异常:

  1. org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xa3

这个数据库(由第三方提供)的编码是SQL_ASCII。0xA3 是'英镑'字符。

因此,我将查询更改为:

  1. SET LOCAL CLIENT_ENCODING TO 'SQL_ASCII'; SELECT * FROM bi_functions;

但它失败了:

  1. 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来避免此错误的方法?

英文:

Using Java 8 and a Postgres 10 Server, and the v 42.2.16 Postgresql Driver,
I have a basic JDBC query function:

  1. public List&lt;Map&lt;String, Object&gt;&gt; Query(String sql) throws Exception {
  2. Connection con = null ;
  3. PreparedStatement pstmt;
  4. List&lt;Map&lt;String, Object&gt;&gt; resultSetToList = null;
  5. try {
  6. con = DriverManager.getConnection(ConnectionString, Properties);
  7. pstmt = con.prepareStatement( sql );
  8. pstmt.execute();
  9. ResultSet resultSet = pstmt.getResultSet();
  10. resultSetToList = resultSetToList(resultSet);
  11. pstmt.close();
  12. } catch(Exception e){
  13. throw e;
  14. }
  15. finally {
  16. if (con != null)
  17. con.close();
  18. }
  19. return resultSetToList;
  20. }

I execute a query like this:

  1. Query(&quot;SELECT * FROM bi_functions&quot;);

But it throws an Exception
> org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xa3

The database (provided by 3rd party) has encoding SQL_ASCII. 0xA3 is the 'British Pound' character.

So I change the query to:

  1. SET LOCAL CLIENT_ENCODING TO &#39;SQL_ASCII&#39;; SELECT * FROM bi_functions;

It fails with:
>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.

Is there a way using plain JDBC to circumvent this error?

答案1

得分: 1

Java在内部使用Unicode编码,因此您不能在JDBC驱动程序中使用与UTF8不同的client_encoding

您应该找出数据库的实际编码(可能是ISO 8859之一或Windows编码)。然后使用该编码创建一个数据库,将原始数据库转储并将转储的数据库加载到新数据库中。

否则您将无法使用JDBC与此数据库进行交互。

英文:

Java uses a Unicode encoding internally, so you cannot use a client_encoding different from UTF8 with the JDBC driver.

You should figure out the actual encoding of the database (probably one of the ISO 8859 or a Windows encoding). Then create a database with that encoding and dump the original database and load your dumped database into it.

Otherwise you won't be able to use this database with JDBC.

huangapple
  • 本文由 发表于 2020年9月21日 12:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63986397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定