JDBC Postgres驱动程序是否有一种方法来设置”client_encoding”以连接到数据库?

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

Does the JDBC Postgres Driver has a way to set the "client_encoding" to connect to the database?

问题

JDBC的Postgres驱动程序是否有设置client_encoding以连接到数据库的方法?

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

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

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

实际上,文档中没有为此目的提供的参数。

我该如何设置输入数据到PG服务器时要使用的编码?

英文:

Does the JDBC Postgres Driver has a way to set the client_encoding to connect to the database?

I am using Spring (with JPA) and the connection information is in the application.properties file:

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

But reading the JDBC docs at https://jdbc.postgresql.org/documentation/head/connect.html, I didn't find a parameter named characterEncoding.

In fact, there is no parameter for this purpose in the docs.

How can I set the encoding to be used when inputting data to the PG server?

答案1

得分: 6

由于Java在内部使用UNICODE编码(UTF-16),因此在PostgreSQL JDBC驱动程序中使用与UTF8不同的client_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"});
  [...]

事实上,如果将客户端编码更改为其他任何值,JDBC驱动程序会毫不含糊地表达不满

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();
      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程序时,您可能会遇到编码转换问题;请尝试在那里解决问题。

英文:

Since Java uses a UNICODE encoding (UTF-16) internally, it would be unnatural to use a client_encoding different from UTF8 in the PostgreSQL JDBC driver.

Consequently, it forces client_encoding to that values, see org.postgresql.core.v3.ConnectionFactoryImpl.getParametersForStartup:

private List&lt;String[]&gt; getParametersForStartup(String user, String database, Properties info) {
  List&lt;String[]&gt; paramList = new ArrayList&lt;String[]&gt;();
  paramList.add(new String[]{&quot;user&quot;, user});
  paramList.add(new String[]{&quot;database&quot;, database});
  paramList.add(new String[]{&quot;client_encoding&quot;, &quot;UTF8&quot;});
  paramList.add(new String[]{&quot;DateStyle&quot;, &quot;ISO&quot;});
  [...]

In fact, if the client encoding is changed to anything else, the JDBC driver expresses its unhappiness in no uncertain terms:

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

  [...]

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

    }
  }

You probably have an encoding conversion problem when you read the data into your Java program; try and fix the problem there.

huangapple
  • 本文由 发表于 2020年10月20日 04:25:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64434691.html
匿名

发表评论

匿名网友

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

确定