简体   繁体   English

如何将JDBC连接到tns oracle

[英]How to connect JDBC to tns oracle

I can connect from plsql to database using tns file 我可以使用tns文件从plsql连接到数据库

Now I want to connect to the database from my Java using JDBC. 现在我想使用JDBC从我的Java连接到数据库。

What I tried: 我尝试了什么:

I search google and I find that I have to using this connection String: 我搜索谷歌,我发现我必须使用此连接字符串:

"jdbc:oracle:thin:@//host:port))/tnsfile)";

My computer name is myPC 我的电脑名是myPC

The port that is written in the tnsfile is 5151 在tnsfile中写入的端口是5151

So I tried this connection String 所以我尝试了这个连接String

"jdbc:oracle:thin:@//myPC:5151))/tnsfile"

but I got this Exception 但我得到了这个例外

java.sql.SQLRecoverableException: IO ERROR: SO Exception was generated

What am I doing wrong? 我究竟做错了什么?

How to connect my JDBC to the database using tns file? 如何使用tns文件将我的JDBC连接到数据库?

You have to set a property named oracle.net.tns_admin to point to the location of the folder containing your tnsnames.ora file. 您必须将名为oracle.net.tns_admin的属性设置为指向包含tnsnames.ora文件的文件夹的位置。 Then you specify the entry from that file after the @ sign in your DB URL. 然后在数据库URL中的@符号后面指定该文件中的条目。 Check example below. 检查下面的例子。 You can find more information here: Data sources and URLs - Oracle Documentation 您可以在此处找到更多信息: 数据源和URL - Oracle文档

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: tnsnames.ora文件中的示例条目:

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. 其中my_net_service_name字符串是我必须从我的Java示例中为ENTRY_FROM_TNSNAMES my_net_service_name字符串。

Rather than hard code the path to tnsnames.ora, better to find it from the environment: 而不是硬编码tnsnames.ora的路径,更好地从环境中找到它:

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);
}

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 一定要有最新版本的ojdbc.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM