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

java连接MS ACCESS,无需配置数据源

java连接microsoft access数据库,无需配置数据源:

?

import java.sql.*;

/**
 * java 连接Microsoft Access数据库,无需配置数据源
 * 
 * @author Administrator
 * 
 */
public class MultiDbConn {

	public static void main(String[] args) {
		String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
		String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=I:/db/member.mdb";
		try {
			Class.forName(dbDriver);
			Connection conn = DriverManager.getConnection(url);

			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from contact");
			ResultSetMetaData rsmd = rs.getMetaData();
			for (int k = 0; k < rsmd.getColumnCount(); k++) {
				System.out.print(rsmd.getColumnName(k + 1)+"\t");
			}
			System.out.println();
			while (rs.next()) {
				for (int k = 0; k < rsmd.getColumnCount(); k++) {
					System.out.print(rs.getString(k + 1) + "\t");
				}
				System.out.println();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}