java-访问中的UTF8 [英] java - UTF8 in access

查看:59
本文介绍了java-访问中的UTF8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我使用下面的代码将记录插入MS Access,并从MS Access获取记录.

但是我可以正确插入或获取UTF8字符串吗?

我插入了nuòiviêtnam ,但它像 ng ?? i vi?t nam
一样存储在MS Access中
你有什么主意吗?
提前谢谢!

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 ; 
     	
    	
    }

推荐答案

您必须对其进行编码和解码.顺便说一句,访问显示的方式仅在屏幕上.只要确保使用正确的编码,您就应该能够将正确的文本读回到您的应用程序中.

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

祝你好运!
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!


这篇关于java-访问中的UTF8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆