![](/img/trans.png)
[英]Getting “java.sql.SQLException: Io exception: The Network Adapter could not establish the connection”
[英]java.sql.SQLException: IO Exception : The Network adapter could not establish the connection?
您好,我正在尝试使用 eclipse 开普勒上的 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");
}
}
}
错误如下。 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)
通过我们的对话,您可以在命令提示符下远程登录并连接到数据库。 经过jdbcurl之后,我发现了错误。
如果xe是数据库的SID,则localhost:1521 / xe应为localhost:1521:xe 。
从更改此代码
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521/xe", "scott", "tiger");
至
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
看看这篇文章
从services.msc启动oracle监听服务
您缺少导入oracle.jdbc.driver.OracleDriver
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.