Home > Back-end >  How to connect to Oracle Database with JDBC
How to connect to Oracle Database with JDBC

Time:09-16

I'm using eclipse to connect to remote database with following details:

name: MSbdd**
Hostname 155.158.xxx.xx
Port: 1521
SID: olt*****

And the authentication type : Default

username: msbd**
password: haslo****

This is the code i Have in eclipse

package net.codejava;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JavaOracleTest {

    public static void main(String[] args) {
        String dbURL = "jdbc:oracle:thin:@localhost:1521:xe";
        String username = "msbd**";
        String password = "haslo****";
        
        try {
            Connection connection = DriverManager.getConnection(dbURL, username, password);
            System.out.println("Połączono z serwerem Oracle");
        } catch (SQLException e) {
            System.out.println("Error");
            e.printStackTrace();
        }

    }

}

And this is the error I get

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=yV 3U5v4TK2js7gMFTixxA==)
Error
    at oracle.jdbc.driver.T4CConnection.handleLogonNetException(T4CConnection.java:882)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:687)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:1086)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:90)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:728)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:649)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at net.codejava.JavaOracleTest.main(JavaOracleTest.java:15)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection (CONNECTION_ID=yV 3U5v4TK2js7gMFTixxA==)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:677)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:568)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:953)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:350)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:2155)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:652)
    ... 7 more
Caused by: java.io.IOException: Connection refused: connect, socket connect lapse 2003 ms. localhost 1521  0 (2/2) true
    at oracle.net.nt.TcpNTAdapter.establishSocket(TcpNTAdapter.java:421)
    at oracle.net.nt.TcpNTAdapter.doLocalDNSLookupConnect(TcpNTAdapter.java:303)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:265)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:238)
    at oracle.net.nt.ConnStrategy.executeConnOption(ConnStrategy.java:902)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:638)
    ... 12 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:482)
    at java.base/sun.nio.ch.Net.connect(Net.java:474)
    at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:694)
    at java.base/java.nio.channels.SocketChannel.open(SocketChannel.java:194)
    at oracle.net.nt.TimeoutSocketChannel.connect(TimeoutSocketChannel.java:184)
    at oracle.net.nt.TimeoutSocketChannel.<init>(TimeoutSocketChannel.java:158)
    at oracle.net.nt.TcpNTAdapter.establishSocket(TcpNTAdapter.java:380)
    ... 17 more

Sorry, for bad formating it's my first time writing here and i'm not the best with programming as you can tell. I've replaced some details with * because it's database from school. Thanks

CodePudding user response:

Your error is:

Caused by: java.net.ConnectException: Connection refused: connect

Check that you have:

  • The correct hostname.

    For example, you say that the hostname is 155.158.xxx.xx but you are using:

    String dbURL = "jdbc:oracle:thin:@localhost:1521:xe";
    

    Should it be:

    String dbURL = "jdbc:oracle:thin:@155.158.xxx.xx:1521:xe";
    
  • the correct port.

  • the correct SID.

    For example, should it be:

    String dbURL = "jdbc:oracle:thin:@155.158.xxx.xx:1521:olt*****";
    
  • the correct username and password.

  • access to the database.

    I.e. you are running it on a network that can access the server and not from a network that cannot access the server (your school may require you to be directly connected to or logged in to their network to access the server and may refuse connections from unauthenticated users outside their network).

  • Related