.mdb文件下载地址:点击打开链接
Java读取.mdb文件不需要导入任何jar,但是JDK有要求:JDK 1.6 32位 点击打开链接 下载
代码一:如下:
package com.accord.util;
import java.sql.*;
import java.util.*;
/**
* Java解析.mdb文件,jdk 1.6 32位
* @author Administrator
*
*/
public class ReadMdbFile {
@SuppressWarnings("static-access")
public static void main(String[] args) {
ReadMdbFile rmf = new ReadMdbFile();
//"src/com/example/filereader/data.txt"
//rmf.readFileACCESS("D://system.mdb");
rmf.readFileACCESS("src/config/system.mdb");
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Map> readFileACCESS(String filePath) {
List<Map> maplist = new ArrayList();
Properties prop = new Properties();
prop.put("charSet", "gb2312"); // 这里是解决中文乱码
prop.put("user", "test0011");
prop.put("password", "test0011");
//String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + filePath; // 文件地址
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + filePath; // 文件地址
//String url = "jdbc:oracle={Microsoft Access Driver (*.mdb)};DBQ=" + filePath; // 文件地址
PreparedStatement ps = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url, prop);
stmt = (Statement) conn.createStatement();
rs = stmt.executeQuery("select * from t_corp");
ResultSetMetaData data = rs.getMetaData();
while (rs.next()) {
Map map = new HashMap();
for (int i = 1; i <= data.getColumnCount(); i++) {
String columnName = data.getColumnName(i); // 列名
String columnValue = rs.getString(i);
System.out.println(columnName + ":" + columnValue);
map.put(columnName, columnValue);
}
maplist.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return maplist;
}
}
运行结果:
ID:1
FCORP:1125
FGSBM:1060337
FGSMC:国药控股广州有限公司惠州大药房鹅岭北路分店
FISMERGE:Y
FSID:fadb
FUSER:testjk
FPWD:test11jk
FBILLTYPE:all
FPROCEDURE:zx_hzpz
FIP:10.10.1.15
代码二:如下:
package com.accord.test;
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 TestMdb {
/**
* TODO : 读取文件access
*
* @param filePath
* @return
* @throws ClassNotFoundException
*/
public static void readFileACCESS(File mdbFile) {
Properties prop = new Properties();
prop.put("charSet", "gb2312"); // 这里是解决中文乱码
prop.put("user", "test0011");
prop.put("password", "test0011");
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("d:/system.mdb"));
readFileACCESS(new File("src/config/system.mdb"));
}
}
运行结果:
1 1125 1060337 国药控股广州有限公司惠州大药房鹅岭北路分店 Y fadb testjk test11jk all zx_hzpz 10.10.1.15
1 1125 1060337 国药控股广州有限公司惠州大药房鹅岭北路分店 Y fadb testjk test11jk all zx_hzpz 10.10.1.15