I can connect from plsql to database using tns file
Now I want to connect to the database from my Java using JDBC.
What I tried:
I search google and I find that I have to using this connection String:
"jdbc:oracle:thin:@//host:port))/tnsfile)";
My computer name is myPC
The port that is written in the tnsfile is 5151
So I tried this connection String
"jdbc:oracle:thin:@//myPC:5151))/tnsfile"
but I got this Exception
java.sql.SQLRecoverableException: IO ERROR: SO Exception was generated
Try the following:
Be sure to have the latest version of ojdbc.jar
You have to set a property named
oracle.net.tns_admin
to point to the location of the folder containing yourtnsnames.ora
file. Then you specify the entry from that file after the@
sign in your DB URL. Check example below. You can find more information here: Data sources and URLs - Oracle DocumentationExample entry from
tnsnames.ora
file:Where
my_net_service_name
string is what you have to subsitite forENTRY_FROM_TNSNAMES
from my Java example.Rather than hard code the path to tnsnames.ora, better to find it from the environment: