Published on Sake.in.th (https://sake.in.th)

หน้าแรก > เรื่องเล่า - การเขียนโปรแกรม > การเก็บภาษาไทยใน MySQL โดยใช้ Java

การเก็บภาษาไทยใน MySQL โดยใช้ Java

  • บล็อกของ sake [1]
  • อ่าน 9445 ครั้ง

วันนี้เขียนจาวาอยู่ เจอปัญหาภาษาไทยเหมือนเดิม อันนี้ก้อบมาคับ ใช้งานได้ดี เหอ ๆ

การเก็บภาษาไทยใน MySQL จะเก็บเป็น ASCII ครับดังนั้นจะต้องมี method ที่จะเปลี่ยนให้เ UNICODE ให้เป็นรหัส ASCII ก่อน insert อะครับ

public class ThaiUtil {

/** Creates a new instance of ThaiUtil */

public static String Unicode2ASCII(String unicode) {

StringBuffer ascii = new StringBuffer(unicode);

int code;

for(int i = 0; i < unicode.length(); i++) {

code = (int)unicode.charAt(i);

if ((0xE01<=code) && (code <= 0xE5B ))

ascii.setCharAt( i, (char)(code - 0xD60));

}

return ascii.toString();

}

public static String ASCII2Unicode(String ascii) {

StringBuffer unicode = new StringBuffer(ascii);

int code;

for(int i = 0; i < ascii.length(); i++) {

code = (int)ascii.charAt(i);

if ((0xA1 <= code) && (code <= 0xFB))

unicode.setCharAt( i, (char)(code + 0xD60));

}

return unicode.toString();

}

}

ข้อมูลจาก : http://www.narisa.com/forums/index.php?showtopic=2738 [2]

Tags: 
programming [3]
Mysql [4]
Java [5]

Source URL (modified on 2008-02-16 06:04): https://sake.in.th/node/14

Links
[1] https://sake.in.th/blogs/sake
[2] http://www.narisa.com/forums/index.php?showtopic=2738
[3] https://sake.in.th/tags/sitetags/programming
[4] https://sake.in.th/category/sitetags/mysql
[5] https://sake.in.th/category/sitetags/java