gpt4 book ai didi

尝试连接数据库时出现 java.net.UnknownHostException

转载 作者:行者123 更新时间:2023-12-03 02:14:02 25 4
gpt4 key购买 nike

我尝试使用以下代码通过 Java 使用 Oracle JDBC 驱动程序连接到 Oracle 数据库(隐藏主机、服务、用户和密码):

import java.sql.*;

public class Main {
public Main () {
try {
String host = "HOST_NAME";
String port = "1521";
String service = "SERVICE_NAME";
String user = "SCHEMA_USER";
String password = "SCHEMA_PASSWORD";

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" +
host +
")(PORT=" +
port +
")))(CONNECT_DATA=(SERVICE_NAME=" +
service +
")))",
user,
password);

connection.close ();

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main (String args) {
new Main ();
}
}

但是,我收到以下错误:

java.sql.SQLException: IO Error: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.acxiom.axle.reporting.database.DatabaseConnection.connect(DatabaseConnection.java:23)
at com.acxiom.axle.reporting.Reporting.establishDatabaseConnection(Reporting.java:53)
at com.acxiom.axle.reporting.Reporting.beginReporting(Reporting.java:20)
at com.acxiom.axle.reporting.Entry.<init>(Entry.java:28)
at com.acxiom.axle.reporting.Entry.main(Entry.java:118)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
... 11 more
Caused by: java.net.UnknownHostException: null
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:117)
at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370)
... 16 more

奇怪的是,我可以从 PL/SQL Developer 连接到数据库,可以 ping 远程主机,并且可以在端口 1521 上 telnet 到远程主机。

为什么只有 Java 会给出 UnknownHostException,但我可以使用其他应用程序连接并 ping 主机?

编辑:我从上面的连接字符串中删除了“hr/hr”。我已尝试按原样连接并将其删除,但仍然收到相同的错误。我还尝试更改连接字符串以匹配他的答案中列出的版本 morgano,得到相同的结果。最后,我尝试将端口号更改为我知道它没有监听的端口,但它仍然收到相同的错误。

最佳答案

连接字符串的格式正确。问题是没有设置主机。它是 null,或者可能是字符串 "null"。根本没有其他方法可以生成错误消息 java.net.UnknownHostException: null

在示例代码中,您可以编写String host = "HOST_NAME";。这使得您在实际代码中看起来像是将常量字符串分配给变量 host。然而,我要坚持说,在你的真实代码中你并没有这样做。您正在从某处查找主机名,此查找由于某种原因失败,您没有检查这一点,因此您将空值传递到连接字符串中。

关于尝试连接数据库时出现 java.net.UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29240616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com