使用java与Oracle数据库连接时出错

时间:2015-02-01 16:49:40

标签: java

    System.out.println("-------- Oracle JDBC Connection Testing ------");
    try
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    }catch(Exception ex)
    {
        System.out.println("Where is your Oracle Driver");
        ex.printStackTrace();
        return;
    }

    System.out.println("Oracle JDBC driver registered");
    try
    {
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott",
                "ahsan");
    }catch(Exception ex)
    {
        System.out.println("Your connection is faild Plz check database");
        ex.printStackTrace();
        return;
    }

与数据库连接时出错 您的连接失败请检查数据库

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 database.main(database.java:30)

2 个答案:

答案 0 :(得分:0)

这可能是由于网络设置中的问题。尝试禁用防火墙并再次尝试。

答案 1 :(得分:0)

请检查您的Oracle侦听器是否配置为侦听端口1521。 打开tnsname.ora或listener.ora以查找哪个主机名(IP地址),什么是端口号以及您要连接的SID是什么。