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

Greek characters in the database are similar to question marks ("?????"). I can not find a solution. I developed an application using Java / Swing, but when I insert Greek letters into MySQL, it looks like question marks. I changed the db sort to utf8 and columns. My project encoding is set to UTF-8. When I insert Greek letters from MySQL, they are saved correctly.

I am using the jar connector file of MySQL.

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

}

+6
source share
1 answer

I think you just need to add a couple more parameters ( useUnicode and characterEncoding ) when creating 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); } } } 
+4
source

Source: https://habr.com/ru/post/956856/


All Articles