67,513
社区成员
发帖
与我相关
我的任务
分享
package com.shengke.tool;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Properties;
public class ReadMdb {
/**
* TODO : 读取文件access
*
* @param filePath
* @return
* @throws ClassNotFoundException
*/
public static void readFileACCESS(File mdbFile) {
Properties prop = new Properties();
prop.put("charSet", "gb2312"); // 这里是解决中文乱码
prop.put("user", "");
prop.put("password", "");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
+ mdbFile.getAbsolutePath();
Statement stmt = null;
ResultSet rs = null;
String tableName = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 连接到mdb文件
Connection conn = DriverManager.getConnection(url, prop);
ResultSet tables = conn.getMetaData().getTables(
mdbFile.getAbsolutePath(), null, null,
new String[] { "TABLE" });
// 获取第一个表名
if (tables.next()) {
tableName = tables.getString(3);// getXXX can only be used once
} else {
return;
}
stmt = (Statement) conn.createStatement();
// 读取第一个表的内容
rs = stmt.executeQuery("select * from " + tableName);
ResultSetMetaData data = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= data.getColumnCount(); i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readFileACCESS(new File("C://Documents and Settings//Administrator//桌面//数据//数据源全国总库2010——2014//a全国2011工商局.mdb"));
}
}