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)
=============================
Then I have a piece of java code
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));
This would give
java length: 16
mysql length: 32
I dont understand why the lengths are different. I explicitly set the database’s character set to UTF16 (which is the default charset of Java, right?) Why do I still get inconsistent length values?
The mySql function “CHAR_LENGTH()” returns the #/Unicode characters (like Java’s “.length”).
The MySQL function “LENGTH()” returns the #/bytes.
Here’s the MySQL reference: