Java JDBC – How to connect to Oracle using tnsnames.ora

I wasn’t even aware that using tnsnames with the thin driver is possible, but apparently it was added somewhere in version 10:

http://docs.oracle.com/cd/B19306_01/java.102/b14355/urls.htm#BEIDIJCE

In particular:

Note:

When using TNSNames with the JDBC Thin driver, you must set the oracle.net.tns_admin property to the directory that contains your tnsnames.ora file.

java -Doracle.net.tns_admin=%ORACLE_HOME%\network\admin

As mentioned, I haven’t checked if this actually works.

I don’t think that the “find the actual network config directory” logic is available via some Oracle function. You’ll have to do it manually as outlined in your question, or maybe rely on the TNS_ADMIN environment variable being present. In that case, the java invocation would be

java -Doracle.net.tns_admin=%TNS_ADMIN%

Leave a Comment