【问题标题】:Does the JDBC Postgres Driver has a way to set the "client_encoding" to connect to the database?JDBC Postgres 驱动程序是否可以设置“client_encoding”以连接到数据库?
【发布时间】:2021-02-02 15:55:32
【问题描述】:

JDBC Postgres 驱动是否有办法设置client_encoding 连接数据库?

我正在使用 Spring(带有 JPA),连接信息在 application.properties 文件中:

spring.datasource.url=jdbc:postgresql://mypgserver.com:5432/mydb?user=myuser&password=mypass&characterEncoding=UTF-8

但是在阅读https://jdbc.postgresql.org/documentation/head/connect.html 的 JDBC 文档时,我没有找到名为 characterEncoding 的参数。

事实上,文档中并没有用于此目的的参数。

如何设置向PG服务器输入数据时使用的编码?

【问题讨论】:

  • 您正在阅读错误的文档!您谈论spring.datasource.url 这是一个弹簧属性,您应该查看弹簧文档而不是 pg 文档。有没有看this Q/A
  • @AbdelghaniRoussi 该属性包含 JDBC 连接。你确定我读错了文档吗?你能给我一个正确的链接吗?
  • JDBC url 的解释是由驱动程序完成的,所以我认为您正在阅读正确的文档。
  • @AbdelghaniRoussi 这个问题是关于 MySQL,而不是 PostgreSQL。连接属性是特定于驱动程序的(除了 JDBC API 中定义的两个属性 userpassword),因此 OP 正在查看正确的文档。

标签: java postgresql jdbc spring-data-jpa character-encoding


【解决方案1】:

由于 Java 在内部使用 UNICODE 编码 (UTF-16),因此在 PostgreSQL JDBC 驱动程序中使用不同于 UTF8client_encoding 是不自然的。

因此,它会将client_encoding 强制设置为该值,请参阅org.postgresql.core.v3.ConnectionFactoryImpl.getParametersForStartup

private List<String[]> getParametersForStartup(String user, String database, Properties info) {
  List<String[]> paramList = new ArrayList<String[]>();
  paramList.add(new String[]{"user", user});
  paramList.add(new String[]{"database", database});
  paramList.add(new String[]{"client_encoding", "UTF8"});
  paramList.add(new String[]{"DateStyle", "ISO"});
  [...]

其实,如果客户端编码改成别的什么,the JDBC driver expresses its unhappiness 毫不含糊:

public void receiveParameterStatus() throws IOException, SQLException {
  // ParameterStatus
  pgStream.receiveInteger4(); // MESSAGE SIZE
  String name = pgStream.receiveString();
  String value = pgStream.receiveString();

  [...]

  if (name.equals("client_encoding")) {
    if (allowEncodingChanges) {
      if (!value.equalsIgnoreCase("UTF8") && !value.equalsIgnoreCase("UTF-8")) {
        LOGGER.log(Level.FINE,
            "pgjdbc expects client_encoding to be UTF8 for proper operation. Actual encoding is {0}",
            value);
      }
      pgStream.setEncoding(Encoding.getDatabaseEncoding(value));
    } else if (!value.equalsIgnoreCase("UTF8") && !value.equalsIgnoreCase("UTF-8")) {
      close(); // we're screwed now; we can't trust any subsequent string.
      throw new PSQLException(GT.tr(
          "The server''s client_encoding parameter was changed to {0}. The JDBC driver requires client_encoding to be UTF8 for correct operation.",
          value), PSQLState.CONNECTION_FAILURE);

    }
  }

当您将数据读入 Java 程序时,您可能遇到了编码转换问题;尝试解决那里的问题。

【讨论】:

  • Java 一开始使用UCS-2。从 5.0 开始,Java 使用 UTF-16 作为内部编码。
  • 谢谢,请找到参考:The native character encoding of the Java programming language is UTF-16. A charset in the Java platform therefore defines a mapping between sequences of sixteen-bit UTF-16 code units (that is, sequences of chars) and sequences of bytes.Charset
  • 再次感谢您的诚意。您当前的答案非常好:-)
猜你喜欢
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
相关资源
最近更新 更多