Wednesday, June 11, 2014

Reading Arabic Data from MS Access Database

Reading Arabic Data from MS Access Database 


Reading and writing data to/from Ms Access Database can be done using JDBC , Hibernate etc frameworks.

Problem: I had a requirement of reading arabic data from ms access database. We tried using hibernate , jdbc .Reading other  than arabic data could be read easily done using hibernate and jdbc .
But for reading Arabic Data is difficult in JDBC and Hibernate . It mostly gives ????? for Arabic data.That means for UTF-8 it was not possible for me . So we went for JACKCESS.

We could use  JACKCESS to read Arabic data for MS Access Database. 



Jackcess is a pure Java library for reading and writing MS Access databases. It provides a robust, cross-platform backend API allowing other developers to integrate MS Access support into their Java applications



Create a  dynamic web project in java.

Download Following Jars :



Put the above jars in your lib folder.

Create a database in microsoft acces with following detail.


Say  a database Name TestDatabase and in it a

table name user .


I have three columns id , name and age .



Now to access the data i have the following code sample :

package com.MSaccessArabicData;

import java.io.File;
import java.util.Map;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
public class MsDatabaseAccess {
 public static void main(String[] args) {
  try {
   Table table = Database
     .open(new File(
       "D:\\TestDatabase.mdb"))
     .getTable("user");
   for (Map row : table) {

    String name = (String) row.get("name");

    int age = (Integer) row.get("age");
    
   }

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}




Documentation for jackcess is available here.








Using Hibernate Synchronizer Tool in Eclipse

Using Hibernate Synchronizer Tool in Eclipse


This tool is used to generate hibernate mapping xml directly from database tables. This is a reverse engineering process where in we directly use database tables to generate so called hbm files. 


Installing it in eclipse 

To use this tool download hibernate synchronizer from following location:

download hibernate synchronizer



Extract the zip file to plugins folder of you eclipse .
Then restart your eclipse .

Then go to windows then to preference.


You will see the hibernate synchronizer option at the left highlighted.



Using it in project


Now go to eclipse and create a project using  File then New and then  Dynamic Web project.


Add mysql jdbc jar to the lib folder of your project.


Now right click your project src folder and create a package say com.lara


Next right click lara folder and go to new then  other and then hibernate mapping file.



suppose following is the database server


suppose following is the database configuration in mysql server .


Container : the folder in which hbm xml needs to be gernerated.

Driver      : the mysql driver class name.


URL         : the url to connect to database . Here test is my database name


Username : mysql username 


password  : mysql password



Table       : select the table to generate Hibernate mapping file .


package   : the package name in here is com.lara . This will be specified in  hbm file generated


and click Finish.





Your hibernate mapping file will be generated inside lara folder.



Generate pojo classes from the hibernate mapping file


Right click the hbm file (eg user.hbm.xml in this case) then Hibernate synchronizer and then synchronize and overwrite as shown below


The java classes will be generated as shown below:

In addition two classes will be generate , one named User in your lara folder and the setters and getters in Base User under base folder in lara package.


Now you can directly use you User class in hibernate for database access.



SOURCE CODE  download from here