英文:
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<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);
}
}
You probably have an encoding conversion problem when you read the data into your Java program; try and fix the problem there.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论