Try connecting to the OCI Database (DBaaS) PDB with Java's JDBC Thin Driver. (Oracle Cloud Infrastructure)

As the title suggests, in the PDB of OCI (Oracle Cloud Infrastructure) Database (DBaaS) Try connecting with Java's JDBC Thin Driver. Try it with the following configuration 彡 (゜) (゜)

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

This is a continuation of the previous article.

Try connecting to the OCI Database (DBaaS) PDB with sqlplus. (Oracle Cloud Infrastructure) https://qiita.com/ora_gonsuke777/items/5029e8cb64fbd9fd62ce

1. Confirmation of connection destination information

Connect to the following PDB.

Hostname (Private): dbname.subnetname.vcnname.oraclevcn.com Port number: 1521 PDB service name: ayspdb2.subnetname.vcnname.oraclevcn.com JDBC URL(thin) :jdbc:oracle:thin:@//dbname.subnetname.vcnname.oraclevcn.com:1521/ayspdb2.subnetname.vcnname.oraclevcn.com

2. Java source code

I am connecting to a PDB and getting the PDB name from the V $ CONTAINERS view. The connection string is the same simple connection as last time (host name: port number / service name) and 彡 (゚) (゚)

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. Compile and run

The usual raw compilation style 彡 (゚) (゚)

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 name

The PDB name is output! 彡 (^) (^)

4. Summary

As long as you know the PDB service name, it's very futuristic (゜) (゜) Next time, I will write an Autonomous DB (ADW / ATP) connection.

Recommended Posts

Try connecting to the OCI Database (DBaaS) PDB with Java's JDBC Thin Driver. (Oracle Cloud Infrastructure)
Try connecting to Autonomous DB (ADW / ATP) with Java's JDBC Thin Driver. (OCI, Oracle Cloud Infrastructure)
Try connecting to the Autonomous Database with JDK6 (Java) + JDBC OCI Driver (type 2).
I tried connecting to Oracle Autonomous Database 21c with JDBC Thin
How to create a JDBC URL (Oracle Database, Thin)
Connecting to a database with Java (Part 1) Maybe the basic method