0

I've been working on this for days, and I have researched everywhere. Anyway, trying to connect my Java code to my SQL database. When using the database development perspective I'm able to connect and make a ping request. The same goes for other tools such a SQL Developer. My Connection settings are similar to the others but it still gives the same error. Any advice on what I could be doing wrong?

import java.sql.*;
import java.io.*;
import oracle.jdbc.*;

public class cars 
{
    private static final String username = "SYSMAN";
    private static final String password = "*";
    public static void insertCar (String id, String name, String model, String type) throws SQLException
    {   
        String sql = "Insert into Cars Values(?,?,?,?)";
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:ORCLPJC", username, password);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, model);
            pstmt.setString(4, type);
            pstmt.executeUpdate();
            pstmt.close();

        }catch(SQLException e) 
        {
            System.err.println(e.getMessage());
        }
    }

    public static void updateCar (String type, String id) throws SQLException
    {
        String sql = "Update cars set type = ?" + "Where id = ?";
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:ORCLPJC", username, password);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, type);
            pstmt.setString(2, id);
            pstmt.executeUpdate();
            pstmt.close();

        }catch(SQLException e) 
        {
            System.err.println(e.getMessage());
        }
    }
    public static void deleteCar(String id) throws SQLException
    {
        String sql = "Delete car from which id = ?";
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:ORCLPJC", username, password);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            pstmt.close();
            }catch (SQLException e)
        {
            System.err.println(e.getMessage());
        }
    }
}

Showing this error:

java.sql.SQLException: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:412)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at OracleDBConnect.main(OracleDBConnect.java:14)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:359)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:422)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:672)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:237)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:141)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:123)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:337)
    ... 12 more
PaulinoC
  • 3
  • 5
  • possible duplicate of https://stackoverflow.com/questions/18037440/the-network-adapter-could-not-establish-the-connection-oracle-11g – Anoop J May 28 '18 at 20:54
  • So, is your database listener really configured for port `1522`? Just asking, because port `1521` is the default and most places leave it as that. what do you get if you run `tnsping ORCLPJC` from the command line? – APC May 28 '18 at 20:54
  • That is what is shows in the tnsnames.ora file. Same settings I used with other tools which works, but it doesn't work with this code – PaulinoC May 28 '18 at 21:00
  • @AnoopJ, I'll look into it. Looks similar, thank you – PaulinoC May 28 '18 at 21:01
  • @AnoopJ, checked it out but the 1st doesn't apply since i started and restarted all oracle services through services.msc. The second one i guess I just don't fully understand what he means – PaulinoC May 28 '18 at 21:11
  • @user5919465 your database TNSListener is started ? If not you can start with the lsnrctl utility. – Anoop J May 28 '18 at 21:16
  • It should have since I'm able to connect to the database using other SQL tools like SQL developer and Eclipse's database development perspective. And i did try to run the lsnrctl utility. Still nothing – PaulinoC May 28 '18 at 21:19
  • @PaulinoC: can you try to connect from SQL developer using a new connection of type Advanced to verify your JDBC URL? – wolφi May 28 '18 at 21:57
  • @wolφi, tried it, this JDBC url, jdbc:oracle:thin:@127.0.0.1:1522:ORCLPJC, worked. Changed it in my code, still same error, but it connected on SQL developer. – PaulinoC May 29 '18 at 08:38
  • @PaulinoC: good, so it's not the listener, not the port, not the tnsnames.ora. – wolφi May 29 '18 at 08:41
  • @wolφi, could it be with eclipse? Maybe the Jave version? I'm using Jdk 1.8 – PaulinoC May 29 '18 at 08:47

1 Answers1

0

Why do you have two different connection strings

jdbc:oracle:thin:@localhost:1522:ORCLPJC

and

jdbc:oracle:thin://localhost:1522/ORCLPJC

Which of the two is actually causing the error ? What happens when you change that to use the other ?

Gary Myers
  • 34,963
  • 3
  • 49
  • 74
  • Changed my code. Tested it on SQL Developer type advanced, it worked with the JDBC URL, changed it in the code above, but same error with the code. There's something wrong with the Java connection to database I guess? – PaulinoC May 29 '18 at 08:41
  • Check out the DataSourceSample(https://github.com/oracle/oracle-db-examples/blob/master/java/jdbc/ConnectionSamples/DataSourceSample.java) on Github for a sample. – Nirmala May 30 '18 at 17:51