Skip to Main Content

Java Database Connectivity (JDBC)

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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How do I successfully pass username/password to jdbc url?

beckyjmcdJan 16 2019 — edited Jan 28 2019

I am having some difficulty creating a jdbc connection when I try to specify the username/password in the URL.  For example, if I have the following code in a try/catch:

String url = "jdbc:oracle:thin:jts_db_user/********@gpdv6.sandia.gov:1525/gpdv6..world";

Connection conn = DriverManager.getConnection(url);

I get an exception:  "java.sql.SQLException: Invalid Oracle URL specified"

However, if I omit the user/name and password from the URL and pass it to the "getConnection" method as follows:

String url = "jdbc:oracle:thin:@gpdv6.sandia.gov:1525/gpdv6..world";

Connection conn = DriverManager.getConnection(url, "jts_db_user", "*******");

I get a valid connection object (and can execute SQL commands against it).

I am using "ojdbc8.jar".

Why am I not able to get a connection when I pass the username/password in the URL?

Comments

Zlatko Sirotic

Try this:

"jdbc:oracle:thin:jts_db_user/*******@gpdv6.sandia.gov:1525:gpdv6..world"

:gpdv6..world instead of /gpdv6..world

Regards,

Zlatko

beckyjmcd

That appears to be the SID style connection.  I just tried that but I am still getting the "Invalid Oracle URL specified".  Interestingly, if I use the ojdbc7.jar file instead of ojdbc8.jar, the SID and Service URLs both works fine.  It appears to be an issue with ojdbc8.jar but all of the documentation indicates you can pass username/password on the URL.

beckyjmcd

I wrote this test code to debug a jdbc connection issue I'm having with a COTS application and that COTS app requires jdbc8.jar.

Zlatko Sirotic

I tried to work with two database versions (11.2.0.4 and 12.1.0.2) and with three versions of ojdbc.jar (ojdbc7.jar, ojdbc8.jar / 12.2, ojdbc8.jar / 18.3).

In all six combinations getConnection(url) works well, with both variants (:SID and /SERVICE_NAME).

I use Java SE 8u202.

Regards,

Zlatko

Gaz in Oz

One issue will be you have 2 dots in your database string, between gpdv6 and world:

   String url = "jdbc:oracle:thin:jts_db_user/********@gpdv6.sandia.gov:1525/gpdv6..world";

   //                                                                              ^

Use a single dot.

beckyjmcd

The service name has two dots.  I tried removing one of the dots but I am still getting:

SQLSTate = "99999"

vendorCode = 17067

detailMessage = "Invalid Oracle URL specified"

cause = (SQLException@1307) "java.sql.SQLException:Invalid Oracle URL specified"

The weird thing is that the 2 dot syntax works fine with ojdbc7.jar.

This is against an Oracle 11.2.04 database but we tried it with a 12.2 database and both give the same error.

It's almost like we have something configured incorrectly at the database level.  My DBA has submitted an Oracle Service Request to help troubleshoot.

beckyjmcd

It's like we have some sort of database configuration issue because I cannot get ojdbc8.jar to work with 11.2.04 or 12.2 but the ojdbc7.jar works fine.  My DBA has submitted an Oracle Service Request to help troubleshoot the issue.

beckyjmcd

This is the configuration for the Oracle listener:

LISTENER11_gpdv6 =

  (ADDRESS_LIST =

    (ADDRESS = (PROTOCOL = IPC)(KEY = gpdv6.world))

    (ADDRESS = (PROTOCOL = IPC)(KEY = gpdv6))

    (ADDRESS = (PROTOCOL = TCP)(Host = gpdv6)(Port = 1525))

  )

PASSWORDS_LISTENER11_gpdv6 = (xxxxxxxxxxxxx)

VALID_NODE_CHECKING_REGISTRATION_LISTENER11_gpdv6 = ON

SID_LIST_LISTENER11_gpdv6 =

  (SID_LIST =

   (SID_DESC =

      (SID_NAME = gpdv6)

      (GLOBAL_DBNAME = gpdv6..world)

    )

  )

Gaz in Oz

The service name has two dots.

Indeed, 2 dot service names seem to be acceptable and work, as you have mentioned.

Setting up a 2 dot service_name for my 11.2.0.2.0 XE db by adding "xe..world" in listener.ora and compiling below java code with

ojdbc8.jar - Oracle JDBC driver: Release 12.2.0.1.0 from instantclient_12_2 and using JDK 1.8.0_144, works.

import java.sql.*;

import java.io.*;

import java.util.*;

public class conn {

   public static void main(String[] argv) throws SQLException, ClassNotFoundException {

      try {

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

         String url = "jdbc:oracle:thin:gaz/gaz@localhost:1521/xe..world";

         System.out.println("Connect String: " + url);

         Connection conn = DriverManager.getConnection(url);

         System.out.println("Connected.");

         conn.close();

         conn = null;

      } catch(SQLException e) {

         // e.printStackTrace();

         System.out.println("ERROR: " + e.getMessage());

      }

   }

}

F:\java>javac conn.java

F:\java>java conn

Connect String: jdbc:oracle:thin:gaz/gaz@localhost:1521/xe..world

Connected.

F:\java>

beckyjmcd

I have had other people say they are able to get the exact same Java code to connect with ojdbc8.jar so I'm thinking there must be an issue on the Oracle server side (maybe a configuration setting) that is causing this not to work.

My DBA has opened a support case with Oracle technical support.  Still waiting for a resolution.  This is very odd.

1 - 10

Post Details

Added on Jan 16 2019
10 comments
28,777 views