0
votes

So I have this oracle database hosted on a Linux server. I know how to access it using putty from a windows machine but I need to access it using JDBC from a windows machine. I have the hostname, port, service name, username and password for the database. I don't understand how to open the connection. I tried it like this:

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@hostname/servicename:port:xe",username,password);

but I am unable to connect. Please help

EDIT:

OK I realized that the address was wrong so I changed it to this: Connection con=DriverManager.getConnection("jdbc:oracle:thin:@hostname:port:servicename",username,password);

now it gives the following error : java.sql.SQLEXCEPTION: The listened refused the connection with following error: ORA-12505 TNS:Listener does not currently know of SID given in connect descriptor.

2
And what happened (besides it didn't connect)? Did you get an error? If so, what was it? Also, the "@hostname/servicename" part. What did you put there? - Elliott Frisch
it gave the following error java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection in the hostname and servicename part I put the machinename/servciename I cant tell you the actual ones. - dhiraj uchil
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "dbSchemaName/userName","password"); should be through. - instinct
Please see the changes in the question - dhiraj uchil

2 Answers

0
votes

Try putting your jdbc url in the format as this

jdbc:oracle:thin:@//localhost:1521/XE
-2
votes

So I managed to correct it. I had written

con=DriverManager.getConnection(
     "jdbc:oracle:thin:@hostname:port:servicename",username,password);

the correct way to do it is

con=DriverManager.getConnection(
     "jdbc:oracle:thin:@hostname:port/servicename",username,password); 

as stated by knight rider.