在线等,hibernate更新对象的属性时乱码,无法插入mysql,很简单的问题编码问题 我的都设的UTF-8啊怎么办?
错误信息:
Hibernate: select movie0_.id as id0_0_, movie0_.daoyan as daoyan0_0_, movie0_.daymovie as daymovie0_0_, movie0_.describe as describe0_0_, movie0_.diqu as diqu0_0_, movie0_.endTime as endTime0_0_, movie0_.image as image0_0_, movie0_.leixing as leixing0_0_, movie0_.name as name0_0_, movie0_.number as number0_0_, movie0_.price as price0_0_, movie0_.startTime as startTime0_0_, movie0_.zhuyan as zhuyan0_0_ from Movie movie0_ where movie0_.id=?
Hibernate: select movie0_.id as id0_0_, movie0_.daoyan as daoyan0_0_, movie0_.daymovie as daymovie0_0_, movie0_.describe as describe0_0_, movie0_.diqu as diqu0_0_, movie0_.endTime as endTime0_0_, movie0_.image as image0_0_, movie0_.leixing as leixing0_0_, movie0_.name as name0_0_, movie0_.number as number0_0_, movie0_.price as price0_0_, movie0_.startTime as startTime0_0_, movie0_.zhuyan as zhuyan0_0_ from Movie movie0_ where movie0_.id=?
你好
Hibernate: update Movie set daoyan=?, daymovie=?, describe=?, diqu=?, endTime=?, image=?, leixing=?, name=?, number=?, price=?, startTime=?, zhuyan=? where id=?
14:18:18,304 WARN org.hibernate.util.JDBCExceptionReporter:100 - SQL Error: 1064, SQLState: 42000
14:18:18,313 ERROR org.hibernate.util.JDBCExceptionReporter:101 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe='浣犲ソ', diqu='鍐呭湴', endTime='2013-03-18 11:22:59.0', image='images' at line 1
14:18:18,320 ERROR org.hibernate.event.def.AbstractFlushingEventListener:324 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
hibernatetemplate 调用的代码:
// TODO Auto-generated method stub
Movie movie=(Movie)ht.get(Movie.class, id);
int number=movie.getNumber();
number=number-1;
movie.setNumber(number);
System.out.println(movie.getDescribe());
ht.saveOrUpdate(movie);
hibernate mysql 乱码 更新
[解决办法]
new String(name.getBytes("iso8859-1"),"utf-8");
[解决办法]
jdbc链接配置的时候加上编码
比如:jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf8
[解决办法]
试试jdbc:mysql://localhost:3306/test??characterEncoding=utf8 看管用不管用
[解决办法]
Mysql编码问题只要保证以下两点就行:
1. Mysql数据库设置为utf8编码
2. 连接时指定为utf8编码
3. 保证SQL语句不是乱码
如果保证了这几点还出现乱码,那只能归属人品问题了,Mysql编码问题应该不会出现这种情况的。不行换台机器重新安装Mysql试试看。
[解决办法]
把SQL语句打印出来,不是乱码就行,如果是乱码,那就跟Mysql没什么关系,是你还没有入库之前就是乱码了,那就是程序编码的问题。
[解决办法]
你用的什么服务器?
[解决办法]
1.jsp
<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
2.容器 response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding(“UTF-8”);
另外就是数据库连接设置jdbc:mysql:localhost:3306/test?useUnicode=true&characterEncoding=utf-8;
如果这些都没问题了,那就是你传的东西跟数据库的字段类型或者长度,个数而引起的
[解决办法]
除了乱码原因造成外,检查一下你实际传过去的值是否超过数据库字段的长度。。