Tuesday, May 1, 2012

Handle Geek characters in an Access database with Java

When reading Greek characters from an access database special action needs to be taken in order to properly display them. The following code prints two string fields named name and surname, stored in an access database:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename = "THE_MDB_FILE";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database += filename.trim() + ";DriverID=22;READONLY=true;useUnicode=true}";
dbConnection = DriverManager.getConnection(database);
Statement s = dbConnection.createStatement();
String query = "Select * from Users";
ResultSet rs = s.executeQuery(query);
String name = new String(rs.getBytes("Name"),"ISO8859_7");
String surname = new String(rs.getBytes("SurName"),"ISO8859_7");
System.out.println("Name:" + name + " Surname:" + surname);