简体   繁体   中英

Can't Connect To Remote Oracle Database (11g)

I'm trying to connect a linux machine which has oracle database 11gr2 setup inside. There is no problem with connecting PL/SQL developer with any user. Unfortunately, with my simple java application, it's impossible to connect to database.

Here is my java code:

JAVA CODE

package oraConn;
import java.sql.*;
public class OraConn {

    public static void main(String[] args) {
    Connection connection=null;
    try {
    String driverName= "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);
    String serverName="192.168.2.122";
    String portNumber="1521";
    String sid="sas";
    String url="jdbc:oracle:thin:@" + serverName + ":" + portNumber +":"+sid;
    String userName = "system";
    String password = "welcome";    
    connection = DriverManager.getConnection(url,userName,password);
    } catch (Exception e) {
    e.printStackTrace();

    }
}
}

When i run this codeblock, i get an exception like this:

java.sql.SQLException: IO Error: Connection refused 
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:489)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:553)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:254)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at oraConn.OraConn.main(OraConn.java:16)
Caused by: oracle.net.ns.NetException: Connection refused 
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:399)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1140)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:340)
    ... 7 more

I am sure that port 1521 is open because i can use PL/SQL developer. What should i do?

Start your program in a debugger and copy the value of url . If you cannot connect with that URL using PL/SQL developer, something with your setting up the url is wrong. Change your program to get the same URL as in your working PL/SQL developer connection, and you should be done.

(This might sound simple, but it worked for me last time I had this problem).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM