UTF8 - Java - MS Access 2003
-
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
-
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
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...
-
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
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[^]