mysql> CREATE database testing CHARACTER SET utf16;
Query OK, 1 row affected (0.00 sec)
mysql> USE testing;
Database changed
mysql> CREATE TABLE t (str varchar(64));
Query OK, 0 rows affected (0.43 sec)
mysql> INSET INTO t values ("1234567891234567");
Query OK, 1 row affected (0.00 sec)
==============================
然后我有一段java代码
Connection connection = DriverManager.getConnection(url, usr, pass);
Statement statement = connection.createStatement();
statement.execute("USE testing");
ResultSet rst = statement.executeQuery("SELECT str, LENGTH(str) FROM t;");
while (rst.next())
System.out.print("java length: " + rst.getString(1).length()
+ "\nmysql length: " + rst.getInt(2));
这会给
java length: 16
mysql length: 32
我不明白为什么长度不同。我明确地将数据库的字符集设置为 UTF16(这是 Java 的默认字符集,对吗?)为什么我仍然会得到不一致的长度值?