【问题标题】:java.sql.SQLException: IO Exception : The Network adapter could not establish the connection?java.sql.SQLException:IO 异常:网络适配器无法建立连接?
【发布时间】:2022-02-15 14:23:24
【问题描述】:

您好,我正在尝试使用 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)

【问题讨论】:

  • 您的机器上是否安装并运行了 oracle?
  • 另外,Oracle TNS 侦听器是否已配置并正在运行?您的 JDBC URL 指向 localhost,因此我们假设您已在本地计算机上运行所有这些。
  • 是的,所有这些都在我的机器上运行我无法理解为什么这些错误是@SlipperySeal。
  • 异常是说这个端口在本地主机上没有打开。您可以使用基于 JDBC 的数据库工具进行连接吗?像 DBVisualizer 等。
  • telnet localhost 1521 命令有效吗?

标签: java eclipse oracle


【解决方案1】:

从我们的对话中,您可以从命令提示符远程登录并连接到数据库。经过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");

看看这个article

【讨论】:

  • 显示 java.sql.SQLException: invalid arguments in call 错误
  • 你能贴出代码sn-p和行号吗?是同一行吗?
  • docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/… 供示例参考。 xe 是您数据库的 SID 吗?
  • 我上面已经有相同的代码,代码没有变化。错误如下 java.sql.SQLException: invalid arguments in call Failed to connect to DB at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at JDBCExample.main(JDBCExample.java:22)
  • 谢谢@ravindra 我明白了。程序成功执行。感谢您的帮助。
【解决方案2】:

从 services.msc 启动 oracle 监听服务

【讨论】:

    【解决方案3】:

    您缺少导入 oracle.jdbc.driver.OracleDriver

    【讨论】:

    • 在堆栈跟踪中很明显oracle.jdbc.driver.OracleDriver 存在并且已被使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    相关资源
    最近更新 更多