java.sql.SQLException:IO异常:网络适配器无法建立连接? [英] java.sql.SQLException: IO Exception : The Network adapter could not establish the connection?

查看:865
本文介绍了java.sql.SQLException:IO异常:网络适配器无法建立连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows 7 OS上使用eclipse kepler上的ojdbc14 jar和Java 8与Oracle 11g数据库建立连接.但是,当我运行代码时,出现以下错误.这是我和相应的错误.

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.

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)

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)

推荐答案

通过我们的对话,您可以通过telnet&从命令提示符连接到数据库.经过jdbcurl之后,我发现了错误.

From our conversation, you are able to telnet & connect to database from command prompt. After going through jdbcurl, I have found the error.

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");

看看此文章

这篇关于java.sql.SQLException:IO异常:网络适配器无法建立连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆