喂,
我尝试将请求发送到具有SQL_ASCII字符编码的PostgreSQL 8.x.遗憾的是我无法以任何方式将其转换为UTF-8:既不使用springframework发送连接属性client_encoding = UTF8,也不使用“SET CLIENT_ENCODING ='UTF8';”直接在jdbc事务中 - 没有任何帮助。
在我检查的jdbc事务中设置客户端编码时,如果确实设置了client_encoding - 是的,则在UTF8中重新设置client_encoding,但是同一会话的下一个语句使我仍然无法识别特殊字符。
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来尝试的 - 发生了同样的错误。
现在真的可以将此编码转换为任何标准吗?
谢谢你的建议!
答案 0 :(得分:1)
的解决方案的
我发现已经是解决方案了。你应该避免转换并直接得到字节:
private String getSqlAscii(ResultSet res, String column) throws SQLException { byte[] b = res.getBytes(column); if(b != null) { try { return new String(b, "[input-encoding]"); } catch (UnsupportedEncodingException e) { log.error("Wrong encoding configured", e); } } return null; }
一个重要的要求:你应该知道,在PostgeSQL的数据输入中使用了Charset是什么。这是SQL_ASCII
的弱点