简体   繁体   English

使用JAVA将希伯来语写入mySql

[英]Writing hebrew to mySql using JAVA

I have a small Java method that inserts short messages to a MySQL Database. 我有一个小的Java方法,可将短消息插入MySQL数据库。 the table's default Collation is utf8_unicode_ci and the java code is: 该表的默认排序规则是utf8_unicode_ci,而Java代码是:

private void insertMessageToDataBase(String lRoom, String lChatusername,
            String lMessage) {
        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/embeddedChat?" +
                "user=site_access&password=XXXXXXX");
            addMessageToDataBase = con.prepareStatement("INSERT INTO `" + lRoom + "` (username, message, action)" +
                    " VALUES (?,?,'message');");
            addMessageToDataBase.setString(1, lChatusername);
            addMessageToDataBase.setString(2, lMessage);
            addMessageToDataBase.executeUpdate();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

the problem is that when lMessage is in hebrew the result is a string of '??????' 问题是,当lMessage是希伯来语时,结果是字符串'??????'

BTW: I don't know if it helps but there is also a PHP script that sometimes write to another similar table in this database and it works fine. 顺便说一句:我不知道它是否有帮助,但是还有一个PHP脚本有时会写入此数据库中的另一个相似表,并且可以正常工作。

Set UTF-8 in your code. 在代码中设置UTF-8。 See this; 看到这个;

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/embeddedChat?useUnicode=true&characterEncoding=utf8");

Thank you Marc B for helping me find out what went wrong. 谢谢Marc B帮助我找出问题所在。 This will be just a summery for any future developer who may fall upon a similar issue. 对于可能遇到类似问题的任何将来的开发人员来说,这只是一个总结。

So, from the comment by Marc (above on my question) I understood I needed to check the link. 因此,从Marc的评论(关于我的问题),我理解我需要检查链接。 I did not know how to do so but I did a brief google search and came upon this page: http://www.jvmhost.com/articles/tomcat-java-mysql-jdbc-and-unicode 我不知道该怎么做,但是我做了一个简短的Google搜索,并找到了以下页面: http : //www.jvmhost.com/articles/tomcat-java-mysql-jdbc-and-unicode

actually all I needed to do was add the following line to the connection string: &useUnicode=true&characterEncoding=UTF-8 实际上,我要做的就是将以下行添加到连接字符串: &useUnicode = true&characterEncoding = UTF-8

this is how my working code looks now: 这是我的工作代码现在的样子:

    private void insertMessageToDataBase(String lRoom, String lChatusername,
            String lMessage) {
        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/embeddedChat?" +
                "user=site_access&password=XXXXXXXX&useUnicode=true&characterEncoding=UTF-8");
            addMessageToDataBase = con.prepareStatement("INSERT INTO `" + lRoom + "` (username, message, action)" +
                    " VALUES (?,?,'message');");
            addMessageToDataBase.setString(1, lChatusername);
            addMessageToDataBase.setString(2, lMessage);
            addMessageToDataBase.executeUpdate();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

It's a mess of course and needs some clean up, but this is a working code. 当然这是一团糟,需要进行一些清理,但这是一个有效的代码。

Thank you Marc B 谢谢马克·B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM