1

I am having a bit of trouble collating Arabic characters into MySQL database using Java.

I am using utf8 for all my tables and my database. Here are some screen shots from Mysql Workbench:

mydatabase has utf8 encoding

keyword table has utf8 encoding The String that I tried to collate is: عماد

It's worth mentioning that Arabic is coded over 2 bytes, so this is clearly not an issue of regular utf8 not being able to handle arabic.

Code for connecting to the database:

String url = "jdbc:mysql://127.0.0.1:3306/mydatabase";
String user = "root";
String passwd = ".........";
String unicode= "?useUnicode=yes&characterEncoding=UTF-8";

setConnection((Connection) DriverManager.getConnection(url+unicode, user, passwd));

Code for inserting the value:

query3 = "INSERT INTO keyword (idkeyword, keyword) VALUES ("+keyWord.getId()+",'عماد')";
Statement state7 = (Statement) connection.createStatement();
state7.executeUpdate(query3);

The exception that I'm receiving:

java.sql.SQLException: Incorrect string value: '\xD8\xB9\xD9\x85\xD8\xA7...' for column 'keyword' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2482)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480)
at controller.DataBaseAccess.saveProject(DataBaseAccess.java:285)

All help is greatly appreciated!

EDIT: The answers in the "“Incorrect string value” when trying to insert UTF-8 into MySQL via JDBC?" are all irrelevant to this case, because they revolve around using utf8mb4, which is not needed for Arabic characters (they are stored over 2 Bytes, not 4 bytes)

DevNight89
  • 33
  • 9
  • What is the encoding of your actual table/database? Can you insert e.g. ASCII characters without seeing this error? – Tim Biegeleisen May 16 '18 at 11:07
  • yeah, I can, I'm using utf8 for the database and the tables – DevNight89 May 16 '18 at 11:09
  • Possible duplicate of ["Incorrect string value" when trying to insert UTF-8 into MySQL via JDBC?](https://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc) – Deb May 16 '18 at 11:12
  • I added some screen shots to clarify encoding. And I don't think this is a duplicate of that topic since Arabic shouldn't need 4 bytes for characters. Regular utf8 works on other Arabic storage cases. Any help figuring out why this isn't the case would be appreciated seeing as just using more space than I actually need doesn't seem like the best solution to my issue. – DevNight89 May 16 '18 at 13:29
  • I also just used this website (https://mothereff.in/utf-8) to convert back the string in the exception, and it seems that it's a 2 byte character encoding, as agreed upon by all other sources I found. Seeing as all answers in the linked answer thread revolve around trying to solve this by using utf8mb4, this does not apply to my case at all. – DevNight89 May 16 '18 at 13:39
  • @DebmalyaBiswas please see comments above. – DevNight89 May 16 '18 at 13:42

0 Answers0