21

I am trying to insert Arabic letter in to mysql database, but it only store "????". I am using DBCP for connecting with mysql databse, here is the datasource:

          <Resource name="jdbc/view_db" 
      auth="Container"
          type="javax.sql.DataSource"
          username="root" 
          password=""
          autoReconnect="true"
          testOnBorrow="true"
          validationQuery = "SELECT 1"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost/view_db"
          maxActive="50"
          maxIdle="10"/>

how to setup UTF-8 encoding with in DBCP configuration or how to use (useUnicode=yes characterEncoding=UTF-8 ) ?

wandarkaf
  • 1,839
  • 20
  • 30
solidfox
  • 581
  • 1
  • 6
  • 20
  • Just checking, did you specify DEFAULT CHARACTER SET utf8 while creating your MySQL Database? – shazin Nov 13 '12 at 11:23
  • yes, I did specify CHARACTER SET utf8, it accept arabic letters when they are entered manually, however, it wont work when I am trying to insert them from jsp file. – solidfox Nov 13 '12 at 11:29

3 Answers3

36

According to the DBCP documentation you need use the parameter connectionProperties with value [propertyName=propertyValue;]*, so in your case you should use something like:

      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost/view_db"
      connectionProperties="useUnicode=yes;characterEncoding=utf8;"
      maxActive="50"

(note the closing ;!)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
12

I tried the following and it worked for me using tomcat 7.0.40 and MySQL 5.5 on Debian Wheezy box:

url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf8" 

Note that & must be represented as &amp;

also make sure that your my.cnf for your mysql server has the following lines:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8
Moneim Emad
  • 121
  • 1
  • 3
7

Specify it in the url parameter, like this:

url="jdbc:mysql://localhost/view_db?useUnicode=yes&amp;characterEncoding=utf8"
Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
  • 1
    I did it, but it said "Could not load the Tomcat server configuration at \Servers\Tomcat v7.0 Server at localhost-config. The configuration may be corrupt or incomplete. The reference to entity "characterEncoding" must end with the ';' delimiter." what is that means ? – solidfox Nov 13 '12 at 11:33
  • 2
    You need to use & in useUnicode=yes&characterEncoding=utf8 – Jim Richards Jun 10 '15 at 10:40