How to connect JDBC to tns oracle

前端 未结 3 977
生来不讨喜
生来不讨喜 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: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);
    }
    

提交回复
热议问题