Java 如何将JDBC连接到tns oracle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19536162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect JDBC to tns oracle
提问by Marco Dinatsoli
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
所以我尝试了这个连接字符串
"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 连接到数据库?
采纳答案by Przemyslaw Kruglej
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
您必须设置一个名为的属性以oracle.net.tns_admin
指向包含您的tnsnames.ora
文件的文件夹的位置。然后在@
登录数据库 URL后指定该文件中的条目。检查下面的示例。您可以在此处找到更多信息:数据源和 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
string 是您必须ENTRY_FROM_TNSNAMES
从我的 Java 示例中替代的内容。
回答by user2213042
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
回答by Greg Chabala
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);
}