0
4

1 回答 1

2

If you want to check the length of your String before inserting it in DB, you have to convert it to the charset used by your DB. It can be done with

 new String(string, charset);

for instance

 System.out.println("‘".length()); 
 // displays 1
 System.out.println(new String("‘".getBytes(), "UTF-16").length()); 
 // displays 2
 System.out.println(new String("‘".getBytes(), "ISO-8859-1").length()); 
 // displays 3

see http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#String%28byte[],%20java.lang.String%29

于 2012-12-13T16:46:25.730 回答