1

I am using servlet with mysql connector 5.1.44 to connect to mysql database.

Connection conn = DriverManager.getConnection(Utils.DBURL+"/"+Utils.DBName+"?useUnicode=true&characterEncoding=utf8mb4&", Utils.DBUserName, Utils.DBPassword);

this works well for multilanguage input but when trying to save emojis, it ends up saving only "??". what can I do to save emoji using just servlet?

servlet code

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
response.setContentType("application/json");
request.setCharacterEncoding("UTF-8");
String content = request.getParameter("content");
Class.forName("com.mysql.jdbc.Driver");
        Connection conn = 
DriverManager.getConnection(Utils.DBURL+"/"+Utils.DBName+"?
useUnicode=true&characterEncoding=utf8mb4&", Utils.DBUserName, 
Utils.DBPassword);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO "
                            + "`feed`(`content`)"
                            + " VALUES (?)");
stmt.setString(1, content);
stmt.executeUpdate();
}

1 Answers1

0

Is the table/database in question set up to store these unicode values? The charset you set in the connection string only controls how data is transfered between client and server. If the column can't store that data you end up with corrupted data.

Lothar
  • 5,323
  • 1
  • 11
  • 27
  • Then some more code showing how exactly you're inserting and selecting the data to/from the database would be of help – Lothar Aug 30 '17 at 22:04
  • i can add emoji to database using phpmyadmin, i can query it successfully too. the only problem is when using servlet for inserting, it becomes ?? , im guessing something is up with servlet – user3136742 Aug 30 '17 at 22:04
  • Check if the characters are received correctly by calling e.g. System.out.println(Character.codePointAt(content, posOfEmoji)); – Lothar Aug 30 '17 at 22:18
  • Without the code showing how you write into the database that's not possible to say. – Lothar Aug 30 '17 at 22:42
  • I'm running out of the usual ideas. Can you try to change the &s by regular &. Maybe it's now the other way around as it was four years ago. Also setting these values using JDBC-Properties might be worth a try. – Lothar Aug 31 '17 at 09:41
  • if i change to regular & it says "Unsupported character encoding 'utf8mb4'" even tried using jdbc properties but same error. what can be a work around, Im guessing something wrong with the driver, maybe if i can save the text as byte and change column to blob would it work, could you point me to some resource if that could work – user3136742 Aug 31 '17 at 12:32
  • I searched again and today I came up with a result that didn't yesterday. You might check out http://info.michael-simons.eu/2013/01/21/java-mysql-and-multi-byte-utf-8-support/ where your problem is described (and emojis are explicitly mentioned ;-) In short: The charsetEncoding seems to be the Java-charset not the MySQL one. The & in your connection-string seems to have the effect that the setting was ignored before so the error-message you get now shows that the setting is now regarded (and since it's invalid it lead to an error). – Lothar Aug 31 '17 at 19:13