项目环境:windows
服务器:tomcat
数据库:mysql5已经设置 default-encoding,database,table以及connection时的encoding都是utf8.
问题1:带有中文的查询语句必须用rs=st.executeQuery(new String(searchSql.getBytes("gbk"),"iso-8859-1"));
如果用rs=st.executeQuery(new String(searchSql.getBytes("gbk"),"utf8"));就报错Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='。。为什么?我认识应该是用iso-8859-1时才报这个错啊,感觉反过来了,想不明白。问题2:查询语句: searchSql = select * from table_xxx a where a.name='唐山',结果出来2条记录,分别是‘唐山’,‘文山’,sql语句改成.... like '%唐山%',或者将唐山改为文山,结果也是一样,如果在like 后面加binary,那么在cc中能正确执行,但是在程序run时查不到任何记录,为什么?如何解决?感谢您的阅读与答复。
服务器:tomcat
数据库:mysql5已经设置 default-encoding,database,table以及connection时的encoding都是utf8.
问题1:带有中文的查询语句必须用rs=st.executeQuery(new String(searchSql.getBytes("gbk"),"iso-8859-1"));
如果用rs=st.executeQuery(new String(searchSql.getBytes("gbk"),"utf8"));就报错Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='。。为什么?我认识应该是用iso-8859-1时才报这个错啊,感觉反过来了,想不明白。问题2:查询语句: searchSql = select * from table_xxx a where a.name='唐山',结果出来2条记录,分别是‘唐山’,‘文山’,sql语句改成.... like '%唐山%',或者将唐山改为文山,结果也是一样,如果在like 后面加binary,那么在cc中能正确执行,但是在程序run时查不到任何记录,为什么?如何解决?感谢您的阅读与答复。
这个是经典问题!!!很明显的你是用了混合编码!!!
在客户端或者可以执行同等命令的方式用 set names latin1或者 set names utf8。
关于第2个问题,具体应该如何解决呢?
不加就能正常写入读出