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

java 连接2003和2007access数据库

???? 由于03和07的access数据库 后缀名不一样,所以连接时需要注意,并且注意windows下面路径的问题,使用转义符转义\,附件为数据文件

?

?

package org.ynu.edu;

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

public class TestMain {

	public static void main(String[] args) {
		String strurl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\mjpDb.accdb";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			conn = DriverManager.getConnection(strurl);
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select id,name,pwd from users");
			while (rs.next()) {
				System.out.print("[" + rs.getInt("id"));
				System.out.print("\t" + rs.getString("name"));
				System.out.println("\t" + rs.getString("pwd") +"]");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		} finally {
			try {
				rs.close();
			} catch (Exception ex) {
			}
			try {
				stmt.close();
			} catch (Exception ex) {
			}
			try {
				conn.close();
			} catch (Exception ex) {
			}
		}

	}
}