Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?

I have inserted nuò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();
    		
    		
    		con.setAutoCommit(false);

    		String sql = " select u1,u2 from tbl_test " ; 
    		
    		PreparedStatement pstm = con.prepareStatement( sql ) ;
    		
    	
    		
    		ResultSet rs  = pstm.executeQuery();
    		

    		while(rs.next()){
    			JOptionPane.showMessageDialog(null, rs.getString("u1")+"--"+ rs.getString("u2"));
    		}
    		    		 
    		
    	}
    	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 ; 
     	
    	
    }
Posted
Updated 8-Aug-10 23:29pm
v2

1 solution

You have to encode and decode them. By the way, the way access show's this is only on screen. You should be able to read the correct text back into your application as long as you ensure the correct encoding is used.

http://download.oracle.com/javase/tutorial/i18n/text/string.html[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900