Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode (Greek) characters are stored in the database like "??????"

Tags:

java

mysql

jdbc

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.

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);
    }
}

}

like image 617
user2109456 Avatar asked Oct 28 '13 10:10

user2109456


1 Answers

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);
        }
    }

}
like image 58
Gord Thompson Avatar answered Oct 12 '22 12:10

Gord Thompson