1 import java.sql.*; 2 3 public class ConnectAccess { 4 5 6 public static void main(String args[]){ 7 ConnectAccess ca = new ConnectAccess(); 8 try { 9 ca.connectAccess(); 10 } catch (Exception e) { 11 e.printStackTrace(); 12 } 13 } 14 15 public void connectAccess() throws Exception { 16 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 17 18 /** 19 * 直接连接access文件。 20 */ 21 String url = "jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=access\\access.accdb;PWD=123456;"; 22 23 /** 24 * 使用ODBC连接数据源 25 */ 26 // String ur1 = "jdbc:odbc:DataSource"; 27 28 Connection conn = DriverManager.getConnection(url, "",""); 29 Statement stmt = conn.createStatement(); 30 String sql = "select * from t_system_user_info"; 31 ResultSet rs = stmt.executeQuery(sql); 32 while (rs.next()) { 33 System.out.println(rs.getString(2)); 34 System.out.println(rs.getString(3)); 35 } 36 rs.close(); 37 stmt.close(); 38 conn.close(); 39 } 40 41 }
仅供学习参考。