java.sql.SQLException:IO 异常:网络适配器无法建立连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33359333/
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
java.sql.SQLException: IO Exception : The Network adapter could not establish the connection?
提问by Naseer Ahammed
Hi I am trying to connect with Oracle 11g database using ojdbc14 jar on eclipse kepler with java 8 on windows 7 os. But when I am running the code I am getting the following error. Here is my and the errors accordingly.
嗨,我正在尝试在 eclipse kepler 上使用 ojdbc14 jar 与 Windows 7 操作系统上的 java 8 连接到 Oracle 11g 数据库。但是当我运行代码时,我收到以下错误。这是我的错误和相应的错误。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter your databse details");
System.out.println("user name");
String uName = sc.next();
System.out.println("password");
String pWord = sc.next();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
// jdbc:oracle:thin:@server:1521:xe
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
System.out.println("Successfully connected to DB");
} else {
System.out.println("Failed to connect to DB");
}
}
}
And the Errors are as follows.
java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JDBCExample.main(JDBCExample.java:23)
错误如下。
java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JDBCExample.main(JDBCExample.java:23)
采纳答案by Ravindra babu
From our conversation, you are able to telnet & connect to database from command prompt. After going through jdbcurl, I have found the error.
从我们的对话中,您可以从命令提示符 telnet 并连接到数据库。经过jdbcurl后,我发现了错误。
localhost:1521/xeshould be localhost:1521:xeif xe is SID for your database.
localhost:1521/xe应该是localhost:1521:xe如果 xe 是您的数据库的 SID。
Change this code from
将此代码更改为
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521/xe", "scott", "tiger");
To
到
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
Have a look at this article
看看这篇文章
回答by nihal
You are missing import oracle.jdbc.driver.OracleDriver
您缺少 import oracle.jdbc.driver.OracleDriver