JDBC Postgres 驱动程序是否有办法设置 "client_encoding" 连接到数据库?
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
但是在 https://jdbc.postgresql.org/documentation/head/connect.html 阅读 JDBC 文档,我没有找到名为 characterEncoding
的参数。
事实上,文档中没有用于此目的的参数。
如何设置向PG服务器输入数据时使用的编码?
由于 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"});
[...]
事实上,如果将客户端编码更改为其他任何编码,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 程序时,您可能遇到了编码转换问题;尝试解决那里的问题。
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服务器输入数据时使用的编码?
由于 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"});
[...]
事实上,如果将客户端编码更改为其他任何编码,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 程序时,您可能遇到了编码转换问题;尝试解决那里的问题。