Search code examples
javamysqlapache-commons-dbcp

how to use (useUnicode=yes characterEncoding=UTF-8 ) with DBCP


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 ) ?


Solution

  • 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 ;!)