3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OCI Database(DBaaS) の PDB に Java の JDBC Thin Driver で接続してみる。(Oracle Cloud Infrastructure)

Last updated at Posted at 2019-02-20

表題の通り、OCI(Oracle Cloud Infrastructure) Database(DBaaS) の PDB に
Java の JDBC Thin Driver で接続してみます。下記の構成で試すやで彡(゚)(゚)

Compute(Java, JDBC Thin Driver) ⇒ (Private Subnet) ⇒ DBaaS(PDB)

前回の記事の続きになります。

OCI Database(DBaaS) の PDB に sqlplus で接続してみる。(Oracle Cloud Infrastructure)
https://qiita.com/ora_gonsuke777/items/5029e8cb64fbd9fd62ce

1. 接続先情報の確認

以下のPDBに接続します。

ホスト名(Private):dbname.subnetname.vcnname.oraclevcn.com
ポート番号    :1521
PDBサービス名  :ayspdb2.subnetname.vcnname.oraclevcn.com
JDBC URL(thin) :jdbc:oracle:thin:@//dbname.subnetname.vcnname.oraclevcn.com:1521/ayspdb2.subnetname.vcnname.oraclevcn.com

2. Java のソースコード

PDBに接続してV$CONTAINERSビューからPDB名を取得してます。
接続文字列は前回同様の簡易接続(ホスト名:ポート番号/サービス名)やで彡(゚)(゚)

import java.sql.*;

public class GetContainerName {
    public static void main(String[] args) {
        final String path = "jdbc:oracle:thin:@//" +
                            "dbname.subnetname.vcnname.oraclevcn.com:" + //hostname
                            "1521/" + //port
                            "ayspdb2.subnetname.vcnname.oraclevcn.com"; //PDB Service
        final String id = "xxxxxxxx";  //ID
        final String pw = "yyyyyyyy";  //password
      
        try (
            Connection conn = DriverManager.getConnection(path, id, pw);
            Statement  stmt = conn.createStatement();
            ResultSet  rs   = stmt.executeQuery("SELECT NAME FROM V$CONTAINERS");
        ) {
            while (rs.next()) {
                String cn = rs.getString("name");
                System.out.println("Container Name => " + cn);
            }
        } catch(SQLException ex) {
            ex.printStackTrace();  //Error
        }
    }
}

3. コンパイルと実行

いつもの生コンパイルなスタイル彡(゚)(゚)

export JAVA_HOME=/home/opc/work/jdk1.8.0_191
export ORACLE_HOME=/home/opc/app/opc/product/18.0.0/client_1
export PATH=${JAVA_HOME}/bin:${PATH}

javac GetContainerName.java
java -classpath .:${ORACLE_HOME}/jdbc/lib/ojdbc8.jar GetContainerName

Container Name => AYSPDB2 ★←PDB名

PDB名が出力されてるで!彡(^)(^)

4. まとめ

PDBのサービス名さえ分かっていれば、至ってフツー彡(゚)(゚)
次回は Autonomous DB(ADW/ATP) の接続を書いてみるやで。

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?