1. 程式人生 > >Java程式生成一個Access檔案

Java程式生成一個Access檔案

package access;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;

import org.junit.Test;

import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;

public class AccessUtil {




public static void main(String[] args) throws IOException {

//這裡同樣支援mdb和accdb
Database db = DatabaseBuilder.create(Database.FileFormat.V2000, new File("d:\\new.mdb"));
Table newTable;

try {
//剛才是建立檔案,這裡是在檔案裡建立表,欄位名,欄位型別
newTable = new TableBuilder("Archives")
.addColumn(new ColumnBuilder("檔案號")
.setSQLType(Types.VARCHAR))
.addColumn(new ColumnBuilder("編制單位")
.setSQLType(Types.VARCHAR))
.addColumn(new ColumnBuilder("案卷正題名")
.setSQLType(Types.VARCHAR))
.addColumn(new ColumnBuilder("案卷題目長度")
.setSQLType(Types.INTEGER))
.addColumn(new ColumnBuilder("檔案盒規格")
.setSQLType(Types.VARCHAR))
.addColumn(new ColumnBuilder("編制單位長度")
.setSQLType(Types.INTEGER))
.toTable(db);
//插入一條資料測試
newTable.addRow("12", "foo","212",44,"323",56);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

@Test
public void operator() throws Exception {
File mdbFile = new File("C:\\Users\\admin\\Desktop\\test.mdb");
if (mdbFile.exists()) {
Database dbin = DatabaseBuilder.open(mdbFile);

Table table = dbin.getTable("Archives");



table.addRow("檔案號 新增測試", "編制單位新增測試", "案卷正題目新增測試",55, "檔案盒規格測試", 5);

}

}
}

 

以下是生成的access檔案開啟後的效果,需用office的access軟體開啟

 

 

 

jackcess-2.1.10.jar