HIBERNATE乱码问题
我在做一个练习的时候后台数据库是Sybase的字符集是cp850用Spring的Hibernate连接后用find()从数据库里取到的值都是乱码,我写了一个方法里到List里把它一项一项的转正了GBK,但是我想问问有没有更好的方法,不用一个一个的转。
下面是applicationContext.xml的代码
<?xml version="1.0" encoding="UTF-8"?><!--- Application context definition for JPetStore's business layer.- Contains bean references to the transaction manager and to the DAOs in- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").- in Spring Train by zhangzhenhua 2007.4.23--><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jee="http://www.springframework.org/schema/jee"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> <bean id="dataSource"jndi-name="java:comp/env/jdbc/sysmanager"/> --><bean id="SessionFactory"read-only="true" rollback-for="Exception"/><tx:method name="find*" read-only="true" rollback-for="Exception"/> <tx:method name="query*" read-only="true" rollback-for="Exception"/><tx:method name="add*" rollback-for="Exception"/><tx:method name="dele*" rollback-for="Exception"/></tx:attributes></tx:advice><aop:config><!-- 事务管理 --> <aop:pointcut id="defaultService" expression="execution(* com.lhd.sysmanager.manager.*.*(..))"/><aop:advisor pointcut-ref="defaultService" advice-ref="TxManager"/></aop:config><!-- Spring集成的hibernate事务管理类 --><bean id="transactionManager" ref="MarketRoleDAO"></property></bean><bean id="MarketUserService" ref="MarketUserDAO"></property></bean><bean id="MarketWorkService" ref="MarketWorkDAO"></property></bean><bean id="PubDeptService" ref="PubDeptDAO"></property></bean><bean id="WorkLogService" ref="WorkLogDAO"></property></bean><bean id="PageService" ref="PageDAO"></property></bean></beans>
public List find() {log.debug("finding MarketWork instance by example");try {List results = getHibernateTemplate().find("from com.lhd.sysmanager.pojo.MarketUser u order by u.id desc");log.debug("find successful, result size: "+ results.size());for(int i= 0;i<result.size();i++) { MarketUser us = (MarketUser)result.get(i); us.setDeptName(PubMethod.ISOtoGBK(us.getDeptName())); System.out.println("#######"+us.getDeptName()+"#####"); }return results;} catch (RuntimeException re) {log.error("find failed", re);throw re;}}<bean id="dataSource" name="code">jdbc:sybase:Tds:10.201.1.111:5000/sgmarketdatabase?useUnicode=true&characterEncoding=GBK但还是不好用,下面是出的信息
WARN JDBCExceptionReporter:54 - SQL Warning: 0, SQLState: 010TP14:32:51,531 WARN JDBCExceptionReporter:55 - 010TP: 服务器无法转换连接的初始字符集 null。将使用服务器建议的字符集 Cp850,并由 jConnect执行转换。14:32:51,546 WARN JDBCExceptionReporter:54 - SQL Warning: 0, SQLState: 010UP14:32:51,546 WARN JDBCExceptionReporter:55 - 010UP: 忽略了无法识别的连接属性 useUnicode。14:32:51,546 WARN JDBCExceptionReporter:54 - SQL Warning: 0, SQLState: 010UP14:32:51,562 WARN JDBCExceptionReporter:55 - 010UP: 忽略了无法识别的连接属性 characterEncoding。14:32:51,578 WARN JDBCExceptionReporter:54 - SQL Warning: 0, SQLState: 010SKjdbc:sybase:Tds:10.201.1.111:5000/sgmarketdatabase?charset=CP850&jconnect_version=5
Cannot create PoolableConnectionFactory (JZ011: 分析连接属性 JCONNECT_VERSION 时遇到异常格式。)
ERROR JDBCExceptionReporter:78 - Cannot create PoolableConnectionFactory (JZ0I5: 指定了一个无法识别的 CHARSET 属性: gbk。)09:24:06,468 WARN SettingsFactory:109 - Could not obtain connection metadataorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (JZ0I5: 指定了一个无法识别的 CHARSET 属性: gbk。)