本文介绍了Unicode(希腊文)字符像"??????"一样存储在数据库中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库中的希腊字符就像问号("?????").我找不到解决方案.我使用Java/Swing开发了一个应用程序,但是当我在MySQL中插入希腊字母时,就像问号一样.我将数据库排序规则也更改为utf8和列.我的项目编码设置为UTF-8.当我从MySQL插入希腊字母时,它们会正确存储.

Greek characters in database are like question marks("?????"). I can't find a solution. I developed an application using Java/Swing but when I insert Greek letters in MySQL are like question marks. I changed db collation to utf8 and the columns too. My project encoding is set to UTF-8. When I insert Greek letters from MySQL then they are stored properly.

我正在使用MySQL连接器jar文件.

I am using MySQL connector jar file.

public class Testing {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    try {
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/chartest","root","1234");
        Statement pst=conn.createStatement();
        String var1="ασδασδ";
        String sql="INSERT INTO chars(value) values('"+var1+"')";
       pst.execute(sql);
    } catch (SQLException ex) {
        Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

推荐答案

我认为您在创建连接时只需要添加几个其他参数(useUnicodecharacterEncoding).以下代码对我有用:

I think you just need to add a couple of more parameters (useUnicode and characterEncoding) when you create the connection. The following code works for me:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class GreekTestMain {

    public static void main(String[] args) {
        String connUrl =
                "jdbc:mysql://192.168.1.3/greektest?" +
                "useUnicode=yes&characterEncoding=UTF-8" +
                "&user=root&password=obfuscated";
        try (Connection conn = DriverManager.getConnection(connUrl)) {
            String sql = "INSERT INTO `chars` (`value`) VALUES (?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, "ασδασδ");
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

}

这篇关于Unicode(希腊文)字符像"??????"一样存储在数据库中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 22:03