读书人

Hibernate修改操作解决思路

发布时间: 2012-04-28 11:49:53 作者: rapoo

Hibernate修改操作
先贴错误信息
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute update queryat org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:110)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:421)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:283)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1169)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)
at com.fyeun.dao.impl.EmployeMessageDaoImpl.updateByEmploye(EmployeMessageDaoImpl.java:53)
at com.fyeun.service.impl.EmployeMessageServiceImpl.updateByEmploye(EmployeMessageServiceImpl.java:37)
at com.fyeun.service.impl.EmployeMessageServiceImpl$$FastClassByCGLIB$$ccc4af60.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at com.fyeun.service.impl.EmployeMessageServiceImpl$$EnhancerByCGLIB$$83200424.updateByEmploye(<generated>)
at com.fyeun.test.Test.main(Test.java:39)
Caused by: java.sql.SQLException: General error message from server: "Incorrect string value: '\xC4\xD0' for column 'sex' at row 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:101)
... 17 more

我是拼接字符串进行修改的 代码

Java code
    public void updateByEmploye( String sex,  Integer age,             String phone,  String address,  String emial,             Integer usernameId) {        // TODO Auto-generated method stub        //Transaction tx;        Session session = this.getSession();        //tx = session.beginTransaction();        String hql = "update Employe set sex='" + sex        + "',age='" + age + "',phone='" + phone        + "',address='" + address + "',emial='" + emial        + "' where usernameId='" + usernameId + "'";        Query query = session.createQuery(hql);        query.executeUpdate();        session.flush();        session.clear();}

测试时候当String类型参数为非中文的时候 ,没错误 ,为中文的时候 报错误了
急死我了 在线等啊

[解决办法]
Caused by: java.sql.SQLException: General error message from server: "Incorrect string value: '\xC4\xD0' for column 'sex' at row 1"


仔细看一下错误信息。
[解决办法]
不正确的string 值。

打印一下你传过来的sex值 就知道。 估计就是乱码
[解决办法]
检查一下映射文件 和 数据库字段类型!
[解决办法]
debug 下
[解决办法]
乱码的问题情况太多了,但一般乱码都不要看报的那些摸不着边的错误
首先,服务器的字符类型是否设置了中文,数据表创建的时候有没有允许中文,你说的情况应该不是服务器的版本兼容情况。
实在不行就写一个转码吧
String str = new String(request.getParameter("something").getBytes("ISO-8859-1"),"utf-8");
如果不是配置问题,老老实实地转码吧
[解决办法]
乱码的问题,以前我也遇到过,最好保持前后台编码格式一致,打印一下值,不行就转码

读书人网 >J2EE开发

热点推荐