JDBC Driver Connection URL

This section describes the connection URL format and how to create connection objects with the DriverManager class.

If you want to use the DriverManager class to create connection objects, you need to know how to make a connection URL that provides access information to the Oracle server. The Oracle connection URL for the thin client-side driver ojdbc6.jar has the following format:

jdbc:oracle:thin:[user/password]@[host][:port]:SID
jdbc:oracle:thin:[user/password]@//[host][:port]/SID

  user - The login user name defined in the Oracle server.

  password - The password for the login user.

  host - The host name where Oracle server is running. 
         Default is 127.0.0.1 - the IP address of localhost.

  port - The port number where Oracle is listening for connection.
         Default is 1521.

  SID  - System ID of the Oracle server database instance. 
         SID is a required value. By default, Oracle Database 10g Express 
         Edition creates one database instance called XE.

Here are some example connection URLs:

jdbc:oracle:thin:Herong/TopSecret@localhost:1521:XE
jdbc:oracle:thin:Herong/TopSecret@:1521:XE

jdbc:oracle:thin:Herong/TopSecret@//localhost:1521/XE
jdbc:oracle:thin:Herong/TopSecret@//:1521/XE
jdbc:oracle:thin:Herong/TopSecret@//localhost/XE
jdbc:oracle:thin:Herong/TopSecret@///XE

I wrote the following program to validate some of the connection URLs listed above:

/* OracleConnectionUrl.java
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
import java.sql.*;
public class OracleConnectionUrl {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Load the Oracle JDBC driver - not needed in JDK 1.8
//      Class.forName("oracle.jdbc.OracleDriver") ;
//      System.out.println("MySQL JDBC driver loaded ok.");

      con = DriverManager.getConnection(
        "jdbc:oracle:thin:Herong/TopSecret@localhost:1521:XE");
      System.out.println("Connected with @localhost:1521:XE.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:oracle:thin:Herong/TopSecret@:1521:XE");
      System.out.println("Connected with @:1521:XE.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:oracle:thin:Herong/TopSecret@//localhost:1521/XE");
      System.out.println("Connected with @//localhost:1521/XE.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:oracle:thin:Herong/TopSecret@//:1521/XE");
      System.out.println("Connected with @//:1521/XE.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:oracle:thin:Herong/TopSecret@//localhost/XE");
      System.out.println("Connected with @//localhost/XE.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:oracle:thin:Herong/TopSecret@///XE");
      System.out.println("Connected with @///XE.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:oracle:thin:Herong/TopSecret@//localhost:1521");
      System.out.println("Connected with @//localhost:1521.");
      con.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Here is the output with JDK 1.8, Oracle 11.2 and ojdbc6.jar:

C:\herong>\Progra~1\java\jdk1.8.0_45\bin\javac OracleConnectionUrl.java

C:\herong>\Progra~1\java\jdk1.8.0_45\bin\java 
   -cp .;\local\lib\ojdbc6.jar
   OracleConnectionUrl
   
Connected with @localhost:1521:XE.
Connected with @:1521:XE.
Connected with @//localhost:1521/XE.
Connected with @//:1521/XE.
Connected with @//localhost/XE.
Connected with @///XE.
Connected with @//localhost:1521.   

As a comparison, when executing the same program with JDK 1.6, Oracle 10.2 and ojdbc14.jar, I got the following:

C:\herong>\Progra~1\java\jdk1.6.0_2\bin\java 
   -cp .;\local\lib\ojdbc14.jar
   OracleConnectionUrl

Connected with @localhost:1521:XE.
Connected with @:1521:XE.
Connected with @//localhost:1521/XE.
Connected with @//:1521/XE.
Connected with @//localhost/XE.
Connected with @///XE.
Exception: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect
descriptor
The Connection descriptor used by the client was:
//localhost:1521

The last URL test failed, because SID was not specified. The Oracle HTTP listener could not connect the request to a database service, the SID.

Last update: 2015.

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

Oracle JDBC Drivers

 Oracle JDBC Drivers Overview

 JDBC Thin Client-Side Driver Installation

 Loading JDBC Driver Class - ojdbc16.jar

JDBC Driver Connection URL

 Creating Connections with DataSource Class

 DataSource Error - makeURL() Failed

 Getting Driver and Server Information

 "CREATE TABLE" - Creating New Tables

 "INSERT INTO" - Inserting New Data Rows

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc42.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 Outdated Tutorials

 References

 PDF Printing Version