Mar
26
2016

Oracle Database Connectivity example in java

Hi in this post we will see JDBC connectivity of oracle DB. I am writing this post because when i was trying it for my own then i faced one problem which i will discuss later in this post. Direct solution to this problem is not available  on stackoverflow or any other website.  Let see

When you will run this program then it will show expected output shown below

User Name for user 1 devbabu password password
User Name for user 1 ankitjain password ankitpass
User Name for user 1 dheeraj password terapasskyah
User Name for user 1 rekha password thakur

The problem that i was facing was because i am used with mysql we give DB name after port number. but in oracle we give service name of oracle. Second problem that i faced is for port number. When i run oracle in browser then i see http://127.0.0.1:8080/apex/. So i thought port number is 8080. But it is not true. The default port number that oracle uses is 1521.  Here is the exception that we got because of wrong port number and without oracle service name. “jdbc:oracle:thin:@localhost:8080:dbusername”

Exception in thread “main” java.sql.SQLException: Io exception: Got minus one from a read call

Then after some google i found out that i was using wrong port number 8080 so i changed url string for correct port number as follow “jdbc:oracle:thin:@localhost:1521:dbusername”  but i still have not mention oracle service name. so i end up with following exception

Exception in thread “main” java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:dbusername

 

then after some google i modified url string for service name of oracle DB as follow and everything worked fine for me.  “jdbc:oracle:thin:@localhost:1521:xe” 

Please Note here xe is service name not DB name which you might be using if you are used to with mysql. 

 

Hope this post will help some one for sure. Have a nice week day. 🙂

About the Author: devender kumar

java/j2EE developer

Comments are closed.