我有一个使用com.mysql.jdbc.Driver连接到MySQL数据库的Web应用程序。 这是jdbcMySQL.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8
jdbc.username=....
jdbc.password=....
my.cnf has these settings:
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
default-character-set = utf8
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
default-character-set = utf8
skip-external-locking
我创建这样的数据库:
CREATE DATABASE dbname CHARACTER SET utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT COLLATE utf8_general_ci;
CREATE TABLE tblname (
`login` VARCHAR(50) NOT NULL,
`description` TEXT
) DEFAULT CHARACTER SET utf8 COLLATE=utf8_general_ci ROW_FORMAT=DYNAMIC;
问题是:如果我做
insert into tblname (login, description) VALUES ('qwe', 'йцукен');
然后我在我的表中
| qwe | йцукен |
但是,如果我做:
jdbcTemplate.update("INSERT INTO work_time (login, date_time) VALUES (?, ?);",
new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, "qwe");
ps.setString(4, "йцукен");
}
}
);
我在桌子上:
qwe | ??????
答案 0 :(得分:1)
MySQL有点疯狂w.r.t.编码,试试:
jdbc.url=jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf-8
此外还有一个可能的错误原因:检查编辑器编码是否与javac编译器编码相同。如果它们不同,您会看到与编译内容不同的内容。
您可以尝试使用u-escaped版本对其进行测试,或查看项目设置:
"\u0439\u0446\u0432\u043a\u0435\u043d"