Forum Stats

  • 3,828,141 Users
  • 2,260,866 Discussions
  • 7,897,490 Comments

Discussions

JDBC Connection strings to connect to Oracle DB

OTG-467455
OTG-467455 Member Posts: 506 Bronze Badge
edited Nov 9, 2017 3:16PM in Java Puzzle Ball

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.

OTG-467455232fbc44-b482-4db7-bdb5-00afaba22a5c

Best Answer

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 8, 2017 9:58AM 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/[email protected]//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();

            }

        }

    }

    OTG-467455OTG-467455232fbc44-b482-4db7-bdb5-00afaba22a5c

Answers

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 8, 2017 9:58AM 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/[email protected]//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();

            }

        }

    }

    OTG-467455OTG-467455232fbc44-b482-4db7-bdb5-00afaba22a5c