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 ; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)