■ ダウンロード先
* commons-dbutils-X.X-bin.zip(ソースは「commons-dbutils-X.X-src.zip」)を落とすhttp://commons.apache.org/proper/commons-dbutils/download_dbutils.cgi
Gradle
compile 'org.dbunit:dbunit:2.5.4'
■ 設定
* 今回は、「commons-dbutils-1.6-src.zip」でソースをインポートする。 1) 「commons-dbutils-1.6-src.zip」を解凍する 2) 「commons-dbutils-1.6-src/src/main/java」配下の「org」とその配下をコピーし、 ソースを貼り付ける
■ サンプル
* 使用するテーブル(PostgreSQL)CREATE TABLE person ( id character(8) NOT NULL, name character varying(100), sex character(1), updatedate timestamp without time zone, CONSTRAINT person_pkey PRIMARY KEY (id) )
SampleDbUtils.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Timestamp; import java.util.List; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; public class SampleDbUtils { public static void main(String[] args) { SampleDbUtils sample = new SampleDbUtils(); sample.execute(); } Connection connection = null; QueryRunner queryRunner = new QueryRunner(); public void execute() { try { Class.forName("org.postgresql.Driver"); try { //DB接続 this.connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/Sample", "user", "password"); // オートコミットを無効 this.connection.setAutoCommit(false); // SELECTサンプル System.out.println("---登録データ---"); List<Person> people1 = this.select("SELECT * FROM person", Person.class); this.print(people1); // INSERTサンプル String query = "INSERT INTO person (id, name, sex, updatedate) VALUES (?, ?, ?, ?)"; this.insert(query, "X0000001", "Tommy", "f", getCurrentTimestamp()); this.insert(query, "X0000002", "Mike", "m", getCurrentTimestamp()); // UPDATEサンプル this.update("UPDATE person SET updatedate = ? WHERE id = ?", getCurrentTimestamp(), "X0000001); // 更新結果確認 System.out.println("---更新結果---"); List<Person> people2 = this.select("SELECT * FROM person", Person.class); this.print(people2); // commit DbUtils.commitAndCloseQuietly(this.connection); } catch (SQLException ex) { DbUtils.rollbackAndCloseQuietly(this.connection); ex.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { // Close処理 DbUtils.closeQuietly(this.connection); } } /** * BeanListHandlerのサンプル */ private <T> List<T> select(String query, Class<T> targetClass) throws SQLException { ResultSetHandler<List<T>> results = new BeanListHandler<T>(targetClass); return (List<T>) this.queryRunner.query(this.connection, query, results); } /** * QueryRunner.updateのサンプル */ private void insert(String query, Object... parameters) throws SQLException { this.queryRunner.update(this.connection, query, parameters); } /** * QueryRunner.updateのサンプル */ private void update(String query, Object... parameters) throws SQLException { this.queryRunner.update(connection, query, parameters); } private Timestamp getCurrentTimestamp() { return new Timestamp(System.currentTimeMillis()); } private void print(List<Person> people) { for (Person person : people) { System.out.println("************"); System.out.println(person.getId()); System.out.println(person.getName()); System.out.println(person.getSex()); System.out.println(person.getUpdatedate()); System.out.println("************"); } } }
参考文献
http://kazunori-kimura.blogspot.jp/2012/05/dbutils.htmlhttp://www.atmarkit.co.jp/fjava/javatips/101java013.html
http://codezine.jp/article/detail/7584