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

后端 未结 5 2065
再見小時候
再見小時候 2020-11-29 08:41

tnsnames.ora file contains the Databases and the their description (host + port).

  • Is it possible to esta

相关标签:
5条回答
  • 2020-11-29 08:57

    Well, in some GUIs the TNS driver configuration is simply not implemented or not working (NetBeans for example :-) )

    https://netbeans.org/bugzilla/show_bug.cgi?id=231526

    There is simple workaround here. You can take the entry directly from the tnsnames.ora file and attach it to the jdbc driver string as following:

    Example from using odbc7.jar (Oracle 12c JDBC driver for JDK 7) to connect to Oracle 11gR2 RAC cluster:

    jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP)(HOST=hostA)(PORT= 1522))(ADDRESS=(PROTOCOL=TCP)(HOST=hostB)(PORT=1521)))(SOURCE_ROUTE=yes)(CONNECT_DATA=(SERVICE_NAME=DatabaseService)))

    Be aware of putting double :: characters in the end as host:port:service, if you will put :: in the end like this:

    jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP)(HOST=hostA)(PORT= 1522))(ADDRESS=(PROTOCOL=TCP)(HOST=hostB)(PORT=1521)))(SOURCE_ROUTE=yes)(CONNECT_DATA=(SERVICE_NAME=DatabaseService)))::

    You will end up with "NL Exception was generated" exception.

    Another approach is to configure following property: System.setProperty("oracle.net.tns_admin","C:/app/product/11.2.0/client_1/NETWORK/ADMIN");

    Of course, instead of hardcoded value, you can for example set up environment variable in your operating system like ORACLE_TNS_ADMIN and then reference it:

    System.setProperty("oracle.net.tns_admin",System.getenv("ORACLE_TNS_ADMIN"));
    

    or pass it to java process via -D switch on linux:

    -Doracle.net.tns_admin=$ORACLE_TNS_ADMIN
    

    and windows:as

    -Doracle.net.tns_admin=%ORACLE_TNS_ADMIN%
    

    Once our application is aware of TNS config file, we can connect by reference service name in TNSNAMES.ora file as in this full example:

     // tell the driver where to look for the TNSNAMES.ORA file
    System.setProperty(
              "oracle.net.tns_admin",
              "C:/app/product/11.2.0/client_1/NETWORK/ADMIN");
    
    // ORCL is net service name from the TNSNAMES.ORA file
    String dbURL = "jdbc:oracle:thin:@ORCL";
    
    // load the driver
    Class.forName("oracle.jdbc.OracleDriver");
    
    Connection conn = null;
    Statement stmt = null;
    
    try {
      conn = DriverManager.getConnection(dbURL,
                                         "your_username",
                                         "your_password");
    
      stmt = conn.createStatement();
    
      ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual");
    
    0 讨论(0)
  • 2020-11-29 09:00

    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%
    
    0 讨论(0)
  • 2020-11-29 09:06

    Firstly make sure that SQL Developer software is properly installed in your machine. If you are using thin driver, ensure that your ojdbcX.jar file is in your build path. The steps to connect to Oracle data source using TNS Alias name are:

    • Set System Property for oracle.net.tns_admin. This should point to the directory which has your tnsnames.ORA file

      System.setProperty("oracle.net.tns_admin", DIRECTORY_PATH_TO_TNSNAME.ORA_FILE);

    • Register an Oracle driver

      DriverManager.registerDriver(new OracleDriver());

    • Create a connection object

      Connection conn = DriverManager.getConnection("jdbc:oracle:thin:username/password@TNS_ALIAS_NAME");

    This should establish the Database connection.

    0 讨论(0)
  • 2020-11-29 09:13

    Starting from 18.3, TNS_ADMIN that provides the location of the tnsnames.ora file can be passed as part of the connection URL. Please note the syntax.

    jdbc:oracle:thin:@jdbctest_medium?TNS_ADMIN=/test/cloud/network
    
    0 讨论(0)
  • 2020-11-29 09:17

    You can also try the following

    Try this, after some hours of troubleshooting came across a sample which I modified and it works like a gem.

    jdbc:oracle:thin:@(description=(address_list=(address=(protocol=tcp)(port=1521)(host=19.16.200.12)) (address=(protocol=tcp)(port=1521)(host=19.16.200.10)))(load_balance = yes)(connect_data=(SERVICE_NAME=stackdb)))
    

    A none load balance sample is given Below:

    jdbc:oracle:thin:@(description=(address_list=(address=(protocol=tcp)
    (port=1521)(host=prodHost)))(connect_data=(INSTANCE_NAME=ORCL)))
    

    Here is the URL that helped https://docs.oracle.com/cd/E11882_01/java.112/e16548/jdbcthin.htm#JJDBC28202

    0 讨论(0)
提交回复
热议问题