JDBC Postgres驱动程序是否有一种方法可以设置"client_encoding"?连接到数据库? [英] Does the JDBC Postgres Driver has a way to set the "client_encoding" to connect to the database?

查看:57
本文介绍了JDBC Postgres驱动程序是否有一种方法可以设置"client_encoding"?连接到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JDBC Postgres驱动程序是否可以设置 client_encoding 来连接数据库?

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

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

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

但是请阅读> https://jdbc.postgresql.org/documentation上的JDBC文档/head/connect.html ,我找不到名为 characterEncoding 的参数.

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.

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

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

推荐答案

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

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.

因此,它强制将 client_encoding 设置为该值,请参见

Consequently, it forces client_encoding to that values, see 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"});
  [...]

实际上,如果客户端编码更改为其他任何内容,则

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

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

这篇关于JDBC Postgres驱动程序是否有一种方法可以设置"client_encoding"?连接到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆