You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决泰语文本插入SQL数据库后显示乱码问题

泰文插入MySQL数据库显示乱码的解决方法

问题场景

  • 泰语文本文件采用ANSI编码
  • MySQL数据库排序规则为utf8_unicode_ci
  • 插入前泰文可正常显示(如:บริษัท)
  • 插入后数据库及读取时泰文显示为??????????????????

原代码

//Connect data base
Connection connect = null;
Statement s = null;    
Class.forName("com.mysql.jdbc.Driver");
connect =  DriverManager.getConnection("jdbc:mysql");
s = connect.createStatement();       

//import txt file             
FileInputStream file = new FileInputStream("text.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
String line;
int i=0;

//loop while read txt file
while((line = reader.readLine())!= null)
{
    int flag=0;
    String sql = "";
    String tbInsert = "";

    //loop for cut | and insert Array               
    String[] split = line.split("\\|");               
    
    for (i = 0; i<split.length; i++)
    {
        //loop for cut black space               
        split[i] = split[i].trim();

        //read data
        if(split[0].substring(0,3).equals("001") && flag==0)
        { 
           
            tbInsert = "header";
            String SQL_INSERT = "INSERT INTO header (Record_Type,Sender,Data_Date,Create_Date,Create_Time) VALUES (?,?,?,?,?)";
            PreparedStatement preparedStatement = connect.prepareStatement(SQL_INSERT);
            
            for (int j = 0; j < 5; j++) {
                preparedStatement.setString(j + 1, new String(split[j]));
            }

            int row = preparedStatement.executeUpdate();
        }

        if(split[0].substring(0,3).equals("999") && flag==0) 
        {
            tbInsert = "summary";
            String SQL_INSERT = "INSERT INTO summary (Record_Type,Record_Type_002,Summary_Record_Type_002,Record_Type_003,Summary_Record_Type_003,Record_Type_004,Summary_Record_Type_004) VALUES (?,?,?,?,?,?,?)";
            PreparedStatement preparedStatement = connect.prepareStatement(SQL_INSERT);

            for (int j = 0; j < 7; j++) {
                preparedStatement.setString(j + 1, new String(split[j]));
            }

            int row = preparedStatement.executeUpdate();
        } 
        flag++;    
    } 
}

解决步骤

1. 正确读取ANSI编码的泰文文件

泰文的ANSI编码对应windows-874(泰国Windows代码页),读取时必须指定编码,避免系统默认编码解析导致乱码:

FileInputStream file = new FileInputStream("text.txt");
// 指定泰文ANSI编码windows-874
BufferedReader reader = new BufferedReader(new InputStreamReader(file, "windows-874"));

2. 强制JDBC连接使用UTF-8编码

在JDBC连接URL中添加字符集参数,确保传输过程中编码一致:

// 替换为实际数据库地址、库名,添加编码参数
connect = DriverManager.getConnection("jdbc:mysql://localhost/your_db_name?useUnicode=true&characterEncoding=utf8");

3. 确认表字段的字符集设置

即使数据库排序规则为utf8_unicode_ci,也要确保表的对应字段字符集为utf8或utf8mb4。可通过以下SQL检查:

SHOW CREATE TABLE header;
SHOW CREATE TABLE summary;

若字段字符集不符,执行修改语句(以header表的Sender字段为例):

ALTER TABLE header MODIFY COLUMN Sender VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
-- 其他存储泰文的字段同理修改

4. 移除不必要的String构造

代码中new String(split[j])属于冗余操作,直接使用原字符串即可,避免额外编码转换:

preparedStatement.setString(j + 1, split[j]);

内容的提问来源于stack exchange,提问作者Mariel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 11:45:34