Writing hebrew to mySql using JAVA

落爺英雄遲暮 提交于 2019-12-01 14:52:19

Set UTF-8 in your code. 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. 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. 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

actually all I needed to do was add the following line to the connection string: &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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!