0

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 screenshots from Mysql Workbench:

and

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!

2
  • Try it with not putting the arabic text directly in a Java file, but loading it from an UTF-8 text file. This error looks a lot like you're compiling your .java file as if it was encoded as latin-1, when it is really encoded as utf-8. If that's the case then you'll need to find how to configure your environment to compile files as encoded in utf-8. But first, we need to establish that it is the real problem. Commented May 16, 2018 at 15:41
  • the text is user input through a JTextField Commented May 16, 2018 at 16:03

1 Answer 1

2

Assuming you are using Java 8 or greater, you could use Base64 to encode the arabic string and store it encoded in the database. This would prevent many other future errors with different type of strings in different languages. When reading the value from the database, you just Base64 decode it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.