Home » Developer & Programmer » JDeveloper, Java & XML » Not possible to connect via Java application (JDBC driver) but SQLplus connects (Oracle XE 19c, Windows 10)
Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690073] Thu, 03 October 2024 17:55 Go to next message
FranCasas
Messages: 6
Registered: October 2024
Junior Member
Hello.

I have developed an application on Java, which uses JDBC driver. Locally (in the server machine) it works perfect, but now, I want to connect via an external net (using remote IP with DDNS).

SQLplus can connect without problem, but my application cannot connect, having the IP correctly changed.

Any ideas about this? I have been searching on posts but I have not find anything. In some sides I have read of needing a listener working on the machine with the application (the one which wants to connect to the remote server), but I have also read that the remote listener on the server must be capable of resolving the remote petitions directly from my application with JDBC.

I also don't know how to set up a listener without installing a DB (I have it working on the remote server machine), if the problem is related to that.

Any comment is appreciated.

Thanks in advance.
Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690074 is a reply to message #690073] Fri, 04 October 2024 00:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68729
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Post your connection string that works with SQL*Plus.
Post your Java code to connect to the database.

Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690077 is a reply to message #690074] Sat, 05 October 2024 07:36 Go to previous messageGo to next message
FranCasas
Messages: 6
Registered: October 2024
Junior Member
Sorry, this is my connection string:

JDBC:
conexion = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)"
+ "(HOST="+localhost+")(PORT="+port+"))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME="+sessionID+")))",user,pw)
;

where:
localhost: string var with connection IP, public IP or DDNS domain
port: used port string, forwarded port
sessionID:XE
user: SYSTEM
pw: bd password

Previously I used with the same result:
conexion = DriverManager.getConnection("jdbc:oracle:thin:@"+localhost+":"+port+":"+sessionID,user,pw);

SQLplus string (working right):
username/password@host:port/service

Where host contents the same as "localhost" previously, and service is XE.

Does this say something to you?

Thanks in advance.
Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690078 is a reply to message #690077] Sat, 05 October 2024 08:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68729
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Sorry, I forgot to ask you which error you got.

But, as far as I know, thin driver does not know tns expression, you have to use oci driver or use "jdbc:oracle:thin:@<serveur>:<port>/<service>" jdbcurl.

I attach a program "JavaTest.java" I use to check my JDBC installation.
Use it in your environment and tell us the result you get.

Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690079 is a reply to message #690078] Sat, 05 October 2024 13:27 Go to previous messageGo to next message
FranCasas
Messages: 6
Registered: October 2024
Junior Member
Hi Michel. Thank you.

Both strings I showed before are working perfectly on local connection, in the server machine.
The problem starts when I use DDNS domain, which fails to connect remotely in my Java app.

I have opened your file and the connection string is the same as the second I posted:
Previously I used with the same result:
conexion = DriverManager.getConnection("jdbc:oracle:thin:@"+localhost+":"+port+":"+sessionID,user,pw);


I don't know where the problem is.

To add some information, I have a log screen on a SSH tunnel built in the server, which redirects traffic to forwarded port to oracle port 1521.
When using SQLplus, log shows the connection attempt, but using my java app there is no attempt in log cmd.

Thanks a lot.
Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690080 is a reply to message #690079] Sat, 05 October 2024 13:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68729
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Note that
username/password@host:port/service
is not the same than
jdbc:oracle:thin:@"+localhost+":"+port+":"+sessionID

The service name is preceded by a "/" not a ":" which indicates a SID not a service name.

You still didn't post the error message or stack which is mandatory to debug.

Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690081 is a reply to message #690080] Sat, 05 October 2024 13:45 Go to previous messageGo to next message
FranCasas
Messages: 6
Registered: October 2024
Junior Member
Hi Michel.
username/password@host:port/service
is for SQLplus, and it works perfect remotely.

The error is:
java.sql.SQLRecoverableException: Error de E/S: The Network Adapter could not establish the connection

Thanks.

Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690082 is a reply to message #690081] Sat, 05 October 2024 14:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68729
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
The error is:
java.sql.SQLRecoverableException: Error de E/S: The Network Adapter could not establish the connection

Yes because you used "jdbc:oracle:thin:@"+localhost+":"+port+":"+sessionID instead of "jdbc:oracle:thin:@"+localhost+":"+port+"/"+sessionID

Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690083 is a reply to message #690082] Sun, 06 October 2024 07:01 Go to previous messageGo to next message
FranCasas
Messages: 6
Registered: October 2024
Junior Member
YOU ARE SO RIGHT!!!!! IT WORKS!!!!!!!!!!

I don't know anyway why locally in the server machine it worked, why?

Michel, so much thanks to you, you don't know how much time I have spent on this, and researched so much.

Thanks a lot, you're the best.
Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690084 is a reply to message #690083] Sun, 06 October 2024 08:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68729
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
I don't know anyway why locally in the server machine it worked, why?

Because local connection does not use the listener to connect.
Your listener, my guess, only contains SERVICE_NAME and not SID_NAME in the SID_DESC part of listener.ora file.
If you add "(SID_NAME=XE)" in this part and restart the listener I think your previous connect string will work as well.

Re: Not possible to connect via Java application (JDBC driver) but SQLplus connects [message #690086 is a reply to message #690084] Sun, 06 October 2024 14:05 Go to previous message
FranCasas
Messages: 6
Registered: October 2024
Junior Member
Thanks for the explanation Michel.

I have copied my listener.ora:
DEFAULT_SERVICE_LISTENER = XE

SID_LIST_LISTENER =
 (SID_LIST =
   (SID_DESC =
 (SID_NAME = XE)

In tnsames.ora it is named as you said:
XE =
 (DESCRIPTION =
   (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
   (CONNECT_DATA =
     (SERVER = DEDICATED)
     (SERVICE_NAME = XE)
   )
 )

I am a newbie in Oracle connections and this is so new for me.

As I said before, so much thanks.
Previous Topic: Jdbc: extract java.time.LocalDate from java.sql.Struct
Next Topic: Error: Could not find or load main class main.java
Goto Forum:
  


Current Time: Mon Dec 30 11:14:28 CST 2024