Search code examples
databaseoracle-databasejdbcoracle12c

Oracle container database 12c, connecting using JDBC


I'm trying to learn how to use Oracle Container database, and just do basic JDBC connections. I installed a dockerised version of Oracle:

https://hub.docker.com/_/oracle-database-enterprise-edition

Which according to the data sheet comes set up with a CDB database called ORCLCDB and a PDB database called ORCLPDB1.

So I figured out I can connect to it like this:

jdbc:oracle:thin:@localhost:1555:ORCLCDB

with username sys, password Oradoc_db1, and setting the special internal_logon jdbc parameter equal to "sysdba" to avoid the error "local oracle CDB: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER"

And I figured out I can switch to the PDB by entering this:

ALTER SESSION SET CONTAINER=ORCLPDB1

And I can then create a new user:

CREATE USER MYUSER IDENTIFIED BY MYPASSWORD1

But then I'm stuck. I think there should be some way to connect directly to the PDB using a JDBC connect string. Every time I google about this, it talks about tnsnames blah blah, but people who use JDBC connections, are typically using Tomcat on a server, or otherwise don't have the Oracle Client installed. They expect to be able to connect to Oracle just with the thin driver installed, nothing else.

I've tried the obvious using:

jdbc:oracle:thin:@localhost:1555:ORCLPDB1 with username myuser or sys, but I always get:

ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

At this point I'm stuck.


Solution

  • You need to use a SERVICE_NAME in order to connect to an Oracle container database

    Please alter your connect string like this:

    jdbc:oracle:thin:@localhost:1555/ORCLPDB1
    
    • A SERVICE_NAME is denoted by a "/"
    • A SID (SystemIDentifier) is denoted by a ":" (not to be used)

    Note! Default listener port is 1521, not sure why you specifically want a different port.

    Best of luck!