Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. UTF8 - Java - MS Access 2003

UTF8 - Java - MS Access 2003

Scheduled Pinned Locked Moved Java
databasejavaquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharkbc
    wrote on last edited by
    #1

    Hi there ! I used bellow code to insert record to MS Access and get record from MS Access. But i could insert or get correctly UTF8 string ? Collapse I have inserted người việt nam but it stored in MS Access like ng??i vi?t nam Have you got any ideas? Thanks in advance!

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.UnsupportedEncodingException;
    import java.sql.Connection;
    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import javax.swing.JOptionPane;

    public class AccessUtil {

    public static String url = "d:/utf-8.mdb";
    private static Connection con_access ;

    private static Connection getConnection() throws Exception {
    Driver d = (Driver)Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    con_access = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + url +";charSet = UTF8");
    return con_access;
    }

    public static Connection getConAccess(){
    try
    {
    if( con_access == null ){
    con_access = getConnection();
    }
    else if( con_access.isClosed() )
    {
    con_access = getConnection();
    }
    }catch(Exception e){
    e.printStackTrace() ;
    }
    return con_access ;
    }

    public static void main(String[] args) {
    try {
    insertTest( "người việt nam", "người việt nam");
    //getTest( );
    //readFile("d:/testacess.txt");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static boolean insertTest( String u1, String u2 )throws Exception {

    Connection con = null ;

    try{

    con = AccessUtil.getConAccess() ;
    Statement s = con.createStatement();

    con.setAutoCommit(false);

    String sql = " insert into tbl_test(u1,u2) values(?,?) " ;

    PreparedStatement pstm = con.prepareStatement( sql ) ;
    pstm.setString(1, u1);
    pstm.setString(2, u2);

    pstm.execute() ;

    con.commit() ;

    }
    catch( Exception e ){

    e.printStackTrace() ;

    try{

    con.rollback() ;
    }
    catch( Exception ex ){}

    throw new Exception( "DB Excetion :"+e.toString() ) ;

    }
    finally{

    try{
    con.close() ;
    }catch(Exception ex){} ;
    }

    return true ;

    }

    public static boolean getTest( )throws Exception {

    Connection con = null ;

    try{

    con = AccessUtil.getConAccess() ;
    Statement s = con.createStatement();

    co

    T J 2 Replies Last reply
    0
    • S sharkbc

      Hi there ! I used bellow code to insert record to MS Access and get record from MS Access. But i could insert or get correctly UTF8 string ? Collapse I have inserted người việt nam but it stored in MS Access like ng??i vi?t nam Have you got any ideas? Thanks in advance!

      import java.io.BufferedReader;
      import java.io.DataInputStream;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.Reader;
      import java.io.UnsupportedEncodingException;
      import java.sql.Connection;
      import java.sql.Driver;
      import java.sql.DriverManager;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.Statement;

      import javax.swing.JOptionPane;

      public class AccessUtil {

      public static String url = "d:/utf-8.mdb";
      private static Connection con_access ;

      private static Connection getConnection() throws Exception {
      Driver d = (Driver)Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
      con_access = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + url +";charSet = UTF8");
      return con_access;
      }

      public static Connection getConAccess(){
      try
      {
      if( con_access == null ){
      con_access = getConnection();
      }
      else if( con_access.isClosed() )
      {
      con_access = getConnection();
      }
      }catch(Exception e){
      e.printStackTrace() ;
      }
      return con_access ;
      }

      public static void main(String[] args) {
      try {
      insertTest( "người việt nam", "người việt nam");
      //getTest( );
      //readFile("d:/testacess.txt");
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }

      public static boolean insertTest( String u1, String u2 )throws Exception {

      Connection con = null ;

      try{

      con = AccessUtil.getConAccess() ;
      Statement s = con.createStatement();

      con.setAutoCommit(false);

      String sql = " insert into tbl_test(u1,u2) values(?,?) " ;

      PreparedStatement pstm = con.prepareStatement( sql ) ;
      pstm.setString(1, u1);
      pstm.setString(2, u2);

      pstm.execute() ;

      con.commit() ;

      }
      catch( Exception e ){

      e.printStackTrace() ;

      try{

      con.rollback() ;
      }
      catch( Exception ex ){}

      throw new Exception( "DB Excetion :"+e.toString() ) ;

      }
      finally{

      try{
      con.close() ;
      }catch(Exception ex){} ;
      }

      return true ;

      }

      public static boolean getTest( )throws Exception {

      Connection con = null ;

      try{

      con = AccessUtil.getConAccess() ;
      Statement s = con.createStatement();

      co

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      This looks like a problem of Access - and yes, other do think so too: [possible answer @ http://bytes.com/ ] greets Torsten

      I never finish anyth...

      1 Reply Last reply
      0
      • S sharkbc

        Hi there ! I used bellow code to insert record to MS Access and get record from MS Access. But i could insert or get correctly UTF8 string ? Collapse I have inserted người việt nam but it stored in MS Access like ng??i vi?t nam Have you got any ideas? Thanks in advance!

        import java.io.BufferedReader;
        import java.io.DataInputStream;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.io.Reader;
        import java.io.UnsupportedEncodingException;
        import java.sql.Connection;
        import java.sql.Driver;
        import java.sql.DriverManager;
        import java.sql.PreparedStatement;
        import java.sql.ResultSet;
        import java.sql.Statement;

        import javax.swing.JOptionPane;

        public class AccessUtil {

        public static String url = "d:/utf-8.mdb";
        private static Connection con_access ;

        private static Connection getConnection() throws Exception {
        Driver d = (Driver)Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
        con_access = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + url +";charSet = UTF8");
        return con_access;
        }

        public static Connection getConAccess(){
        try
        {
        if( con_access == null ){
        con_access = getConnection();
        }
        else if( con_access.isClosed() )
        {
        con_access = getConnection();
        }
        }catch(Exception e){
        e.printStackTrace() ;
        }
        return con_access ;
        }

        public static void main(String[] args) {
        try {
        insertTest( "người việt nam", "người việt nam");
        //getTest( );
        //readFile("d:/testacess.txt");
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }

        public static boolean insertTest( String u1, String u2 )throws Exception {

        Connection con = null ;

        try{

        con = AccessUtil.getConAccess() ;
        Statement s = con.createStatement();

        con.setAutoCommit(false);

        String sql = " insert into tbl_test(u1,u2) values(?,?) " ;

        PreparedStatement pstm = con.prepareStatement( sql ) ;
        pstm.setString(1, u1);
        pstm.setString(2, u2);

        pstm.execute() ;

        con.commit() ;

        }
        catch( Exception e ){

        e.printStackTrace() ;

        try{

        con.rollback() ;
        }
        catch( Exception ex ){}

        throw new Exception( "DB Excetion :"+e.toString() ) ;

        }
        finally{

        try{
        con.close() ;
        }catch(Exception ex){} ;
        }

        return true ;

        }

        public static boolean getTest( )throws Exception {

        Connection con = null ;

        try{

        con = AccessUtil.getConAccess() ;
        Statement s = con.createStatement();

        co

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        sharkbc wrote:

        But i could insert or get correctly UTF8 string ?

        The Sun jdbc-odbc bridge does not support UTF-16 which is how MS Access 2003 does unicode. Possible solutions - Find a commercial jdbc-odbc bridge that supports it - Find the commercial MS Access jdbc driver. Far as I know there is only one that does direct versus ODBC implementation. - Use a different database. - Encode the data in the database. That of course means it would not be viewable in anything except the java application. Following bug documents this. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6345277[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups