Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK

This is a sample Java code that retrieves and displays the DBLINK source and destination data in an Oracle Database using DBLINK. When I was asked "Is there a source (source)?" In a certain question in a certain chat, it became "Gununu.", So I wrote it. 彡 (゜) (゜)

1. Environment and configuration

I tried to verify with the following configuration.

Java → (JDBC) → User_A → (DBLINK) → User_B

The environment uses the following environment of Virtualbox. It's easy to set up because it's included from the beginning. 彡 (゜) (゜)

Effortlessly build an Oracle DB 19c environment with OTN's VirtualBox image https://qiita.com/ora_gonsuke777/items/b41f37637e59319796b4

2. Create User_A and insert data

First, create User_A and insert the data.

export ORACLE_HOME=/u01/app/oracle/product/version/db_1
export PATH=${PATH}:${ORACLE_HOME}/bin

sqlplus /nolog

CONNECT SYS/oracle@ORCL AS SYSDBA

CREATE USER USER_A IDENTIFIED BY USER_A
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP;

GRANT CREATE SESSION TO USER_A;
GRANT CREATE TABLE TO USER_A;
ALTER USER USER_A QUOTA UNLIMITED ON USERS;

CONNECT USER_A/xxxxxxxx@orcl;

CREATE TABLE TBL_A(
  COL1 NUMBER
);
INSERT INTO TBL_A VALUES(100);
COMMIT;


Connected.

User created.

Grant succeeded.

Grant succeeded.

User altered.

Connected.

Table created.

1 row created.

Commit complete.

3. Create User_B and insert data

Then create User_B and insert the data. What to do is with User_A 彡 (゚) (゚)

export ORACLE_HOME=/u01/app/oracle/product/version/db_1
export PATH=${PATH}:${ORACLE_HOME}/bin

sqlplus /nolog

CONNECT SYS/oracle@ORCL AS SYSDBA

CREATE USER USER_B IDENTIFIED BY USER_B
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP;

GRANT CREATE SESSION TO USER_B;
GRANT CREATE TABLE TO USER_B;
ALTER USER USER_B QUOTA UNLIMITED ON USERS;

CONNECT USER_B/xxxxxxxx@orcl;

CREATE TABLE TBL_B(
  COL1 NUMBER
);
INSERT INTO TBL_B VALUES(200);
COMMIT;


Connected.

User created.

Grant succeeded.

Grant succeeded.

User altered.

Connected.

Table created.

1 row created.

Commit complete.

4. DBLINK creation and operation check

Create DBLINK in User_A and check the operation. You can see that the TBL_B created for User_B can only be referenced via DBLINK.


export ORACLE_HOME=/u01/app/oracle/product/version/db_1
export PATH=${PATH}:${ORACLE_HOME}/bin

sqlplus /nolog

CONNECT SYS/oracle@ORCL AS SYSDBA

GRANT CREATE DATABASE LINK TO USER_A;

CONNECT USER_A/xxxxxxxx@orcl;

CREATE DATABASE LINK DBL_USER_B
CONNECT TO USER_B IDENTIFIED BY xxxxxxxx
USING 'ORCL';

SELECT * FROM TBL_A;

SELECT * FROM TBL_B;

SELECT * FROM USER_B.TBL_B;

SELECT * FROM TBL_B@DBL_USER_B;

Connected.

Grant succeeded.

Connected.

Database link created.

      COL1
----------
       100

              *
ERROR at line 1:
ORA-00942: table or view does not exist

                     *
ERROR at line 1:
ORA-00942: table or view does not exist

      COL1
----------
       200

5. Java code sample

This is a sample that connects with User_A and SELECTs and displays TBL_A and TBL_B (via DBLINK) respectively.

import java.sql.*;

public class GetDblinkData {
    public static void main(String[] args) {
        final String path = "jdbc:oracle:thin:@localhost:1521/orcl";
        final String id = "USER_A";  //ID
        final String pw = "xxxxxxxx";  //password

        try (
            Connection conn  = DriverManager.getConnection(path, id, pw);
            Statement  stmt  = conn.createStatement();
            ResultSet  rs    = stmt.executeQuery("SELECT COL1 FROM TBL_A");
            Statement  stmt2 = conn.createStatement();
            ResultSet  rs2   = stmt2.executeQuery("SELECT COL1 FROM TBL_B@DBL_USER_B");
        ) {
            while (rs.next()) {
                int i = rs.getInt("COL1");
                System.out.println("TBL_A COL1 => " + i);
            }
            while (rs2.next()) {
                int j = rs2.getInt("COL1");
                System.out.println("TBL_B COL1 => " + j);
            }
        } catch(SQLException ex) {
            ex.printStackTrace();  //Error
        }
    }
}

5. Compile and run

Well, finally compile and run. When you execute the following command ... 彡 (゚) (゚)

javac GetDblinkData.java

java -classpath /u01/app/oracle/product/version/db_1/jdbc/lib/ojdbc8.jar:. GetDblinkData

TBL_A COL1 => 100
TBL_B COL1 => 200

The DBLINK original data (TBL_A) and DBLINK destination data (TBL_B) could be acquired and displayed! 彡 (^) (^)

6. Summary

I was able to confirm that the data of the DBLINK source and the DBLINK destination can be acquired with a single DB connection. However, DBLINK is a double-edged sword. It's a tightly coupled mechanism, so don't abuse it. 彡 (゜) (゜)

Recommended Posts

Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
Java source sample (Oracle Database + java) to SELECT and display CLOBs
Sample code to get the values of major SQL types in Java + Oracle Database 12c
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
Sample code to convert List to List <String> in Java Stream
[For beginners] Minimum sample to display RecyclerView in Java
How to convert A to a and a to A using AND and OR in Java
Sample code to parse date and time with Java SimpleDateFormat
Source to display character array with numberPicker in Android studio (Java)
[Android] Convert Map to JSON using GSON in Kotlin and Java
Sample code to call the Yahoo! Local Search API in Java
Sample to read and write LibreOffice Calc fods file in Java 2021
Display Firestore data in RecyclerView [Java]
Java 9 new features and sample code
Sample code using Minio from Java
Try to get data from database using MyBatis in Micronaut + Kotlin project
Using Hystrix and Sentinel in code example
[Java] Flow from source code to execution
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Sample source code for finding the least common multiple of multiple values in Java
Sample code to get the values of major SQL types in Java + MySQL 8.0
Technology for reading Java source code in Eclipse
How to display a web page in Java
Code to escape a JSON string in Java
[Java] Convert DB code to code value using enum
Try using Sourcetrail (win version) in Java code
Try using Sourcetrail (macOS version) in Java code
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Call a stored procedure in java's CallableStatement interface to see the PROGRAM and MODULE columns in V $ ACTIVE_SESSION_HISTORY. (Oracle Database + java)