Sep 19, 2005

[Derby] Derby にチャレンジ

Derby とは

Derby は 元 CloudScape と呼ばれた RDBMS。 IBM が Apache に寄贈したもの。 Pure Java で DBMS 自体のサイズが 2M と小さいのがステキ。

Derby
http://db.apache.org/derby/

Derby の実行環境構築

  1. Derby(derby.jar) をクラスパスに追加

サンプルコード

package jp.in_vitro.codelets.derby;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Codelet {

    private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";

    private static final String PROTOCOL = "jdbc:derby:";

    public Codelet() {
        super();
    }

    public static void main(final String[] args) throws InstantiationException,
            IllegalAccessException, ClassNotFoundException, SQLException {

        Codelet me = new Codelet();
        me.execute();
    }

    protected void execute() throws InstantiationException,
            IllegalAccessException, ClassNotFoundException, SQLException {

        // Derby を embedded モードで起動する。
        this.initializeDBMS();
        try {
            try {
                // テーブルの生成。
                this.createTable();
            } catch (SQLException e) {
                // 既に Table が生成済みの可能性があるので・・・
            }
            // insert
            this.insertRecord();
            // update
            this.updateRecord();
            // select
            this.selectRecord();
            // delete
            this.deleteRecord();
            // テーブルの削除。
            this.dropTable();
        } finally {
            // Derby のシャットダウン
            this.destroyDBMS();
        }
    }

    protected void initializeDBMS() throws InstantiationException,
            IllegalAccessException, ClassNotFoundException, SQLException {
        Class.forName(DRIVER).newInstance();

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(PROTOCOL
                    + "derbyDB;create=true");
            connection.commit();

        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    protected void destroyDBMS() throws SQLException {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(PROTOCOL
                    + ";shutdown=true");
            connection.commit();

        } catch (SQLException e) {
            // Derby が正常終了すると SQLException が発行される。
            System.out.println("" + e.getLocalizedMessage() + " : "
                    + e.getErrorCode());
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    protected Connection getConnection() throws SQLException {

        Connection connection = null;
        Properties props = new Properties();
        props.put("user", "me");
        props.put("password", "password");

        connection = DriverManager.getConnection(PROTOCOL + "derbyDB", props);
        connection.setAutoCommit(false);

        return connection;
    }

    protected void createTable() throws SQLException {

        Connection connection = this.getConnection();
        try {
            Statement statement = connection.createStatement();
            statement
                    .executeUpdate("CREATE TABLE sample(field01 INT, field02 VARCHAR(10))");

            connection.commit();
            connection.close();

        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    protected void insertRecord() throws SQLException {

        Connection connection = this.getConnection();
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO sample VALUES (999,'dummy data')");
            statement.executeUpdate("INSERT INTO sample VALUES (888,'hogehoge')");

            connection.commit();
            connection.close();

        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    protected void updateRecord() throws SQLException {

        Connection connection = this.getConnection();
        try {
            Statement statement = connection.createStatement();
            statement
                    .executeUpdate("UPDATE sample SET field01=100 WHERE field02='dummy data'");

            connection.commit();
            connection.close();

        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    protected void deleteRecord() throws SQLException {

        Connection connection = this.getConnection();
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate("DELETE FROM sample");

            connection.commit();
            connection.close();

        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    protected void selectRecord() throws SQLException {

        Connection connection = this.getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement
                    .executeQuery("SELECT * FROM sample ORDER BY field01");

            while (rs.next()) {
                System.out.println("field01=" + rs.getString(1) + ", field02="
                        + rs.getString(2));
            }

            rs.close();
            connection.commit();
            connection.close();

        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    protected void dropTable() throws SQLException {

        Connection connection = this.getConnection();
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate("DROP TABLE sample");

            connection.commit();
            connection.close();

        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

}

Posted in Derby | このエントリーをはてなブックマークに追加 | この記事をクリップ! livedoor クリップ |