Create a gist now

Instantly share code, notes, and snippets.

@cofearabi /Database0
Last active Nov 13, 2015

What would you like to do?
jackcess sample for creating database
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
public class Database0 {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
try {
// mdb ファイルの作成
Database db = new DatabaseBuilder(new File("/home/xxxx/Dropbox/doc/mdb/jack.mdb"))
.setFileFormat(Database.FileFormat.V2000)
.create();
// テーブルを作成
Table table = new TableBuilder("Test")
.addColumn(new ColumnBuilder("ID", DataType.LONG)
.setAutoNumber(true))
.addColumn(new ColumnBuilder("Name", DataType.TEXT))
.addColumn(new ColumnBuilder("Salary", DataType.MONEY))
.addColumn(new ColumnBuilder("StartDate", DataType.SHORT_DATE_TIME))
.toTable(db);
// データをテーブルに入力
BigDecimal money_value=new BigDecimal("123000.0");
table.addRow(1, "john");
table.addRow(2, "taro",12300.0);
table.addRow(3, "mary",money_value,new SimpleDateFormat("yyyy-MM-dd").parse("2015-11-14"));
// 別のテーブルも作成
Table newTable = new TableBuilder("NewTable")
.addColumn(
new ColumnBuilder("col1").setSQLType(Types.INTEGER)
.toColumn())
.addColumn(
new ColumnBuilder("col2").setSQLType(Types.VARCHAR)
.toColumn())
.addColumn(
new ColumnBuilder("col3").setSQLType(Types.CHAR)
.toColumn()).toTable(db);
// データをテーブルに入力
newTable.addRow(1, "bar","thaa");
newTable.addRow(2, "foo","wer");
// データの取得
for (Map<String, Object> map : newTable) {
System.out.println(
"col1 : " + map.get("col1") +
" col2 : " + map.get("col2") +
" col3 : " + map.get("col3")
);
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment