Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDBC Connection strings to connect to Oracle DB

OTG-467455Nov 8 2017 — edited Nov 9 2017

What are the various acceptable JDBC connection strings that can be used to connect to an Oracle database?

Which connection string is commonly used?

Please explain what the purpose of each different connection string is.

The reason for my asking this is I have seen some connection string incarnations I did not think could work.

This post has been answered by mNem on Nov 8 2017
Jump to Answer

Comments

mNem
Answer

You may find the answers from

Tutorial:

https://docs.oracle.com/database/121/TDPJD/getconn.htm#TDPJD144

Documentation:

https://docs.oracle.com/database/121/JJDBC/urls.htm#JJDBC28267

If you need a minimal class to test things out (copied from net and modified):

Play around with the variations of url and see the messages when it fails....

import java.sql.*;

public class Test1 {

    public static void main(String[] args) throws Exception {

        Connection conn = null;

        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");

            conn = DriverManager.getConnection(

                    "jdbc:oracle:thin:ora1/ora1@//localhost:1521/pdborcl", null, null);

            Statement stmt = conn.createStatement();

            ResultSet rset = stmt.executeQuery("select sysdate from dual");

            if (rset.next()) {

                System.out.println(String.format(">>>>> Server time : [%s]", rset.getString(1)));

            }

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println(e.getMessage());

        } finally {

            System.out.println(String.format("Using connect url : [%s]", conn.getMetaData().getURL()));

            if (conn != null) conn.close();

        }

    }

}

Marked as Answer by OTG-467455 · Sep 27 2020
1 - 1

Post Details

Added on Nov 8 2017
1 comment
2,896 views