LoginSignup
2
1

More than 5 years have passed since last update.

Java apache-derby 接続 [ commons-dbutils ]

Posted at

メモ

Java MySQL 接続 [ commons-dbutils ] を derby 用に変更 (マアカワラナイ)
加算時に明示的にキャストしないとエラーになる。


import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.derby.jdbc.EmbeddedSimpleDataSource;

public class Main {

    public static final String URL = "jdbc:derby:test";
    public static final String DBNAME = "test";
    public static final String USER = "";
    public static final String PASS = "";

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

        MapHandler mapHandler = new MapHandler();

        EmbeddedSimpleDataSource dataSource = new EmbeddedSimpleDataSource();

        dataSource.setDatabaseName(DBNAME);
        dataSource.setConnectionAttributes("create=true");
        dataSource.setUser(USER);
        dataSource.setPassword(PASS);

        QueryRunner runner = new QueryRunner(dataSource);

        for (int i = 0, j = 10; i < 10; i++, j = j << 2) {
            System.out.println(runner.query(
                    "SELECT ( cast( ? as DECIMAL(10,0) ) + "
                            + "cast( ? as DECIMAL(10,0) ) ) "
                            + "AS ANS FROM (values 1 ) as t", mapHandler, i, j)
                    .get("ANS"));
        }
    }
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1