2015年4月9日木曜日

[PostgreSQL]JDBCの接続URLでカレントスキーマを指定する

JDBCで接続先のURLを指定するときに、接続パラメータでカレントのスキーマを指定する。

URLのフォーマット

カレントスキーマの接続パラメータは、currentSchemaなので、以下のようにURLの最後に指定する。

jdbc:postgresql://localhost/test?currentSchema

実行例

// 以下のコードの場合、カレントスキーマはtest_schemaとなる
dataSource.setUrl("jdbc:postgresql://localhost/test?currentSchema=test_schema");
try (Connection connection = dataSource.getConnection()) {
    final String schema = connection.getSchema();
    System.out.println("schema = " + schema);       // -> schema = test_schema
}