How to connect JDBC to tns oracle

前端 未结 3 958
生来不讨喜
生来不讨喜 2020-11-29 08:49

I can connect from plsql to database using tns file

Now I want to connect to the database from my Java using JDBC.

What I tried:

I search google an

相关标签:
3条回答
  • 2020-11-29 09:12

    You have to set a property named oracle.net.tns_admin to point to the location of the folder containing your tnsnames.ora file. Then you specify the entry from that file after the @ sign in your DB URL. Check example below. You can find more information here: Data sources and URLs - Oracle Documentation

    import java.sql.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        System.setProperty("oracle.net.tns_admin", "C:/app/product/11.2.0/client_1/NETWORK/ADMIN");
        String dbURL = "jdbc:oracle:thin:@ENTRY_FROM_TNSNAMES";
    
        Class.forName ("oracle.jdbc.OracleDriver");
    
        Connection conn = null;
        Statement stmt = null;
    
        try {
          conn = DriverManager.getConnection(dbURL, "your_user_name", "your_password");
    
          System.out.println("Connection established");
    
          stmt = conn.createStatement();
    
          ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual");
    
          if (rs.next()) {
            System.out.println(rs.getString(1));
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        finally {
          if (stmt != null) try { stmt.close(); } catch (Exception e) {}
          if (conn != null) try { conn.close(); } catch (Exception e) {}
        }
      }
    }
    

    Example entry from tnsnames.ora file:

    my_net_service_name= 
     (DESCRIPTION= 
       (ADDRESS=(some address here))
       (CONNECT_DATA= 
         (SID=some_SID_name)))

    Where my_net_service_name string is what you have to subsitite for ENTRY_FROM_TNSNAMES from my Java example.

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

    Try the following:

    System.setProperty("oracle.net.tns_admin", PATH_TO_TNSNAMES.ORA);
    Class.forName ("oracle.jdbc.OracleDriver");
    dbUrl = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST="+IPHOST+")(PORT="+PORT+"))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME="+DBNAME+")))"
    
    conn = DriverManager.getConnection(dbUrl, USERNAME, PASSWORD);
    

    Be sure to have the latest version of ojdbc.jar

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

    Rather than hard code the path to tnsnames.ora, better to find it from the environment:

    public static void setTnsAdmin() {
        String tnsAdmin = System.getenv("TNS_ADMIN");
        if (tnsAdmin == null) {
            String oracleHome = System.getenv("ORACLE_HOME");
            if (oracleHome == null) {
                return; //failed to find any useful env variables
            }
            tnsAdmin = oracleHome + File.separatorChar + "network" + File.separatorChar + "admin";
        }
        System.setProperty("oracle.net.tns_admin", tnsAdmin);
    }
    
    0 讨论(0)
提交回复
热议问题