8

I have a java application, want to insert arabic words to mysql database, my code looks like

Connection con = null;
    String url = "jdbc:mysql://localhost/";
    String db = "students";
    String driver = "com.mysql.jdbc.Driver";
    try {
        Class.forName(driver);
        con = DriverManager.getConnection(url+db,"root","");
        Statement st = con.createStatement();
        String name = new String(txtName.getText().getBytes(), "UTF-8");
        int val = st.executeUpdate("insert into student(name, roll) VALUES('"+name+"','"+txtRoll.getText()+"')");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

But it only insert '??????'. what can i do now?

Pritom
  • 1,294
  • 8
  • 19
  • 37
  • Are you sure that ????? is in the database? It may be that whatever you're using to view the record can't display Arabic. Write some Java code to read the record back out and compare it to the original data. – Paul Dec 08 '11 at 15:25
  • 2
    Also, double-check your table is using the `UTF-8` charset (or any other that can handle arabic characters). – Romain Dec 08 '11 at 15:27
  • 1
    Also, use a [`PreparedStatement`](http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html) to avoid SQL injection attacks! – maerics Dec 08 '11 at 15:28
  • Yeah, it also reads ????? in console. – Pritom Dec 08 '11 at 15:30
  • Agree with @Romain check that the table in your database is using the correct character encoding. – JRSofty Dec 08 '11 at 15:36
  • I am using `utf8_bin` as collation of table cell, which you mention? but nothing happens. – Pritom Dec 08 '11 at 15:39
  • Don't check it in the console, use the String equals method and print true or false to the console. – Paul Dec 08 '11 at 15:42
  • No they are not equal. `if(rs.getString(1).equalsIgnoreCase("كيف")) { System.out.println("YES"); }` – Pritom Dec 08 '11 at 15:55
  • But this `if(rs.getString(1).equalsIgnoreCase("???")) { System.out.println("YES"); }` print YES – Pritom Dec 08 '11 at 15:59

4 Answers4

24

add this variable after db variable declaration:

String unicode= "?useUnicode=yes&characterEncoding=UTF-8";

and then modify your line:

con = DriverManager.getConnection(url+db,"root","");

to

con = DriverManager.getConnection(url+db+unicode,"root","");
1

i faced the same problem but with Chinese data, and it is not only the UTF8 in the db connection is the solution you should also do this:

Ensure that your MySQL configuration encoding is defined correctly. Add these lines to either my.cnf "in the case if using linux" or my.ini "case you using windows"

[client] 
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-character-set=utf8
character-set-server=utf8
Medhat
  • 1,622
  • 16
  • 31
  • I know this is an old post but still.... I attempted setting in the `my.cfg` as you suggested. There appears to be an issue with the entry `default-character-set=utf8` in the mysqld section. There server would not start. – FDavidov Jun 25 '16 at 13:31
  • So far no. Moreover, the additional double-quote stopped appearing. – FDavidov Jun 27 '16 at 10:59
0

@maerics I tried using PreparedStatement instead and SQL Injection attack is still able to be done, I think either am implementing the PreparedStatemnt wrong or your feed is incorrect.

E_X
  • 3,722
  • 1
  • 14
  • 15
0

You have to create your database as a UTF-8 table otherwise no matter what you add it will never be able to store it...

second you need to encode it before you save it, I like to use unicode... one thing about this is that if you have a column that specifies 32 length, you will have to increase that for unicode as a single character takes up 6 characters,

example

letter h appears as U+0068

  • For your kind information, I let you know that, I want any language to insert such english, chineese etc as example. I dont know which language in my text box. – Pritom Dec 08 '11 at 15:44
  • @Pritom, as long as you're using UTF-8 you'll be fine - there's nothing language-specific in his answer. – Paul Dec 08 '11 at 15:46
  • Think that table is already created and need to modified, what can I do now please. I am using utf8_unicode_ci as Collation for each row of table structure, is that ok or not? – Pritom Dec 08 '11 at 15:52
  • If you need Chinese, `utf8mb4` (and collation `utf8mb4_unicode_ci`) would be better than `utf8`. – Rick James May 05 '15 at 23:49