2006/08/25

[Java]讀取MS Access資料

//設定Connection的URL
String filepath = "./test.mdb";
String url = "jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ=" + filepath;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//取得Access的Connection
Connection objAccessConnection = DriverManager.getConnection(url);
DatabaseMetaData objMeta = objAccessConnection.getMetaData();
//取得Access的Table Schema
ResultSet objTables = objMeta.getTables(null, null, null, new String[]{"TABLE"});
//取得Access中的所有表格
while (objTables.next()) {
    //表格名稱
    String strTableName = objTables.getString("TABLE_NAME");
    //查詢的SQL句
    String strQuerySql = "SELECT * FROM " + strTableName;
    //查詢用的Statement
    Statement objAccessStatement = objAccessConnection.createStatement();
    //取得資料集
    ResultSet objData = objAccessStatement.executeQuery(strQuerySql);
}

沒有留言: