看了benq的中文问题终极解决之道,执行了下面步骤:
JSP文件头:
<%@ page contentType="text/html;charset=utf-8" %>
getParameter之前:
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
request时:
String name1= new String(request.getParameter("username"));
String pwd1= new String(request.getParameter("password"));
连接mysql时:
jdbc:mysql://localhost/mydb?useUnicode=true&characterEncoding=utf-8
配置文件:
mysql在windows\my.ini里default-character-set=utf-8
mysql里数据库和表也都是utf-8
软件版本:
Apache Tomcat 5.5.6 Server
phpMyAdmin 2.6.4-pl4
MySQL 5.0.19-nt
至此,在页面上显示中文和从数据库读取并显示中文都正常。
但还剩最后一个问题,就是getParameter之后向数据库写入中文时出现下面错误:
java.sql.SQLException: Syntax error or access violation, message from server: "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
''asdf' at line 1"
但写入英文没有任何问题。。
SQL语句如下:
String strSQL="INSERT INTO login ( username , password ) VALUES ('"+name1+"','"+pwd1+"')";
还有哪里有问题?是Tomcat的编码问题吗??
请大家帮忙解决。谢谢了。