【发布时间】:2011-04-09 18:52:10
【问题描述】:
你好,
我尝试将请求发送到具有字符编码 SQL_ASCII 的 PostgreSQL 8.x。遗憾的是,我无法以任何方式将其转换为 UTF-8:既不能使用 springframework 发送连接属性 client_encoding=UTF8,也不能“SET CLIENT_ENCODING = 'UTF8';”直接在 jdbc 事务中 - 没有任何帮助。
在我检查过在 jdbc 事务中设置客户端编码时,如果真的设置了 client_encoding - 是的,client_encoding 确实设置在 UTF8 中,但是同一会话的下一条语句返回我仍然无法识别的特殊字符。
con = ds.getConnection();
con.setAutoCommit(false);
stmt = con.prepareStatement("SHOW client_encoding");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1));
//Here is the output "UNICODE"
}
stmt.close();
stmt = con.prepareStatement("SET client_encoding='UTF8'");
stmt.execute();
stmt.close();
stmt = con.prepareStatement("SHOW client_encoding");
rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1));
//Here is the output "UTF8"
}
stmt.close();
stmt = con.prepareStatement(sql);
ResultSet res = stmt.executeQuery();
while(res.next()) {
String s = res.getString("mycolumn");
System.out.println(s);
//The text has "?" instead of special chars
}
配置:
<bean id="mybean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<constructor-arg><value>[myurl]</value></constructor-arg>
<constructor-arg>
<props>
<prop key="charSet">SQL_ASCII</prop>
<prop key="client_encoding">UTF-8</prop>
<prop key="timezone">UTC</prop>
<prop key="user">[user]</prop>
<prop key="password">[password]</prop>
<prop key="allowEncodingChanges">true</prop>
</props>
</constructor-arg>
<property name="driverClassName"><value>org.postgresql.Driver</value></property>
</bean>
使用的 PostgreSQL-Driver 是 postgresql-8.4-701.jdbc4.jar
PostgreSQL 中的输入是 LATIN1,我也尝试通过将编码设置为 LATIN1 和 ISO88591 - 发生同样的错误。
现在真的可以将此编码转换为任何标准吗?
谢谢你的建议!
【问题讨论】:
标签: java spring postgresql jdbc character-encoding