大家好!
最近做一个项目要用到mysql。可是在jsp中修改中文记录时总是出现 Incorrect string value: '\xC3\xA5\xC2\xA4\xC2\xA7...'这样的问题。
在mysql的dos下可以修改,但总时问号。查了好多资料都没有解决。烦请各位高手帮帮小弟,小弟先谢过了。
项目的具体描述是这样的:
--------------------------
操作系统: windowsXP SP2
mysql版本:mysql-5.1.23-rc-win32
mysql-connector-java-5.1.6
eclipse版本:3.3.1.1
-------------------------
mysql> show variables like 'character_set_%';
+--------------------------+------------------------------------------+
| Variable_name            | Value                                    |
+--------------------------+------------------------------------------+
| character_set_client     | utf8                                     |
| character_set_connection | utf8                                     |
| character_set_database   | utf8                                     |
| character_set_filesystem | binary                                   |
| character_set_results    | utf8                                     |
| character_set_server     | utf8                                     |
| character_set_system     | utf8                                     |
| character_sets_dir       | D:\mysql-5.1.23-rc-win32\share\charsets\ |
+--------------------------+------------------------------------------+
8 rows in set (0.00 sec)mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.01 sec)mysql> show create database m08001;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| m08001   | CREATE DATABASE `m08001` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)mysql> show create table calendar;
| Table    | Create Table
| calendar | CREATE TABLE `calendar` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `eId` int(10) unsigned NOT NULL,
  `cDate` date NOT NULL,
  `cTime` varchar(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_calendar_eId_employee_id` (`eId`),
  CONSTRAINT `FK_calendar_eId_employee_id` FOREIGN KEY (`eId`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)mysql> select * from calendar;
+----+-----+------------+-------+
| id | eId | cDate      | cTime |
+----+-----+------------+-------+
| 17 |   2 | 2008-04-24 | ??    |
| 18 |   2 | 2008-04-26 | ??    |
| 19 |   2 | 2008-04-27 | ??    |
| 20 |   3 | 2008-04-27 | ??    |
| 22 |   2 | 2008-04-21 | ??    |
+----+-----+------------+-------+
5 rows in set (0.00 sec)mysql> update calendar set cTime = '二十点至六点' where id=20;
Query OK, 1 row affected, 1 warning (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from calendar;
+----+-----+------------+------------+
| id | eId | cDate      | cTime      |
+----+-----+------------+------------+
| 17 |   2 | 2008-04-24 | ??         |
| 18 |   2 | 2008-04-26 | ??         |
| 19 |   2 | 2008-04-27 | ??         |
| 20 |   3 | 2008-04-27 | ?????????? |
| 22 |   2 | 2008-04-21 | ??         |
+----+-----+------------+------------+
5 rows in set (0.00 sec)--------------------------
eclipse中通过con.createStatement().executeQuery("SHOW VARIABLES LIKE 'character_set_%'")查询如下:character_set_client,utf8
character_set_connection,utf8
character_set_database,utf8
character_set_filesystem,binary
character_set_results,utf8
character_set_server,utf8
character_set_system,utf8
character_sets_dir,D:\mysql-5.1.23-rc-win32\share\charsets\连接字符串 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/m08001?useUnicode=true&characterEncoding=utf-8","root", "root");
/**
* 修改员工出勤信息
*/
public void updateCalendar(String workTime,String id){
        //构建查询字符串
sql = "UPDATE calendar SET cTime = '"+workTime+"' WHERE id ="+id;
System.out.println("sql is "+ sql);
//如果连结断开则打开连结
try{
    if(con.isClosed()){
getConnection();
    }

    stmt = con.prepareStatement(sql);
    stmt.executeUpdate();
}catch(Exception e){
    e.printStackTrace();
}
}EmployeeCalendar.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<% request.setCharacterEncoding("utf-8"); %>EmployeeCalendarServlet.java
response.setCharacterEncoding("utf8");
数据更新的代码片段:
String id = request.getParameter("tid");
String workTime = request.getParameter("t"+tid);
calendarOperation.updateCalendar(workTime, id);基本上情况就是这样,那位高手能帮帮我,很急。