日期:2014-05-16  浏览次数:20441 次

java使用jdbc轻松连接访问access

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TestAccess {
	public static void main(String[] args) {
		String path = TestAccess.class.getResource("testaccess.mdb").getPath().substring(1);
		//String strurl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=src\\test\\testaccess.mdb";
		String strurl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+path;
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection conn = DriverManager.getConnection(strurl);

			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from user");
			while(rs.next()){
				System.out.print(rs.getString("id")+"\t");
				System.out.print(rs.getString("name")+"\t");
				System.out.println(rs.getString("password"));
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
?