Thursday, August 13, 2009

How to connect Java Application with Oracle Database using JDBC Driver - Part 1

Today I will go through the basic steps to connect a Java Application with Oracle Database using JDBC Driver (Java Database Connectivity). To develop Enterprise Product connectivity of java application and oracle database is essential. When I was new to java, i have faced some problem for java and database connectivity using jdbc. So I want to make all this process bit easier for all the new comer in java and windows platform.






Prerequisites :
Steps for configuring the system and creating DSN (ODBC Data Source):
  1. Install Java Development Kit and Oracle Database.
  2. Place Oracle JDBC Driver (*.jar file) in java runtime folder and java development kit folder . eg: 'C:\Program Files\Java\jre1.6.0\lib\ext' and 'C:\Program Files\Java\jdk1.6.0\jre\lib\ext'.
  3. Create a Data Source Name (DSN) required for database connectivity in windows.
  4. For DSN go to Start->Control Panel->Administrative Tools->Data Sources (ODBC).
  5. ODBC Data Source Administrator will open. Click on the Add button to create a new data source.
  6. Select 'Microsoft ODBC for Oracle' & click Next.
  7. Fill the Data Source Name - name for the data source, Description - Just a simple description, User Name - Oracle Database User name, Server - name of the server where database is installed ,( localhost - if it is installed in the local machine or the IP if the the database is installed in a remote machine). Click OK and close the ODBC Data Source Administrator.

Now, the system is configured to run any java oracle connectivity program.You can start coding the program in any IDE or open a notpad.

Below is the source code of a jdbc program to determine wheather all your setting are OK or not. Compile the program and execute it. If the program is executed without any error or exception and you get 'JDBC Driver loaded' and 'Connected to the Oracle Database' as output then you have done all the setting correctly and you are able to connect to the oracle database through your java program.
If you encounter any problem please get back to me, I will help you to sought out the problem.


Source code :

import java.sql.*;

public class ConnectOracle {

public static void main(String[] args) {
String driver="sun.jdbc.odbc.JdbcOdbcDriver"; //
String cs="jdbc:odbc:connectOracle"; //connectOracle is the data source name
String user = "system"; //username of oracle database
String pwd = "tom"; //password of oracle database
Connection con = null; //connection variable assigned to null
try
{
Class.forName(driver);// for loading the jdbc driver
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("JDBC Driver loaded");
try
{
con=DriverManager.getConnection(cs,user,pwd);// for establishing connection with database
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Connected to the Oracle Database");
try
{
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}//end of main()
}//end of class()


In my next post we will see how to insert data into Oracle Database through your java program.[link]

Reblog this post [with Zemanta]




Bookmark and Share

36 comments:

Anonymous said...

pls help me,I connected netbeans to oracle...
how to read data from data base on performing some action..

Bishwajeet said...

@badri: can i know what problem you are facing or mail me your problem , I can help you

Anonymous said...

thanks yar..

Anonymous said...

i did whatever u explained but still i m getting classnotFound exception.
please tell me how to abt it

Anonymous said...

thanx for great text.
I m using shine enterprise pattern and I just write a line to connect to database.
I make an JDBC object and then call Login.
I got shine from here http://sourceforge.net/projects/shine-enterpris/
It is an open source pattern for j2ee,j2se,j2me application.

Unknown said...

i used this shine pattern it is good

i suggest you to work with it.
it worth

Anonymous said...

sir u describes the type 1 jdbc connectivity ,but while using netbeans type 4 connectivity i m ahving the problem of connecting oraacl with my jdbc program.. :(

Unknown said...

Hi,I am a student of BCS

I follow your instruction to connect oracle to java application.

I use your given program to check whether connection is established or not?

It is compiled without any error,but when In run program it gives exception :

Exception in thread "main" java.lang.NoClassDefFoundError:ConnectOracle

My program is as follows :

It is present in bin directory

import java.sql.*;

public class ConnectOracle {

public static void main(String[] args) {
String driver="sun.jdbc.odbc.JdbcOdbcDriver"; //
String cs="jdbc:odbc:connectOracle"; //connectOracle is the data source name
String user = "XXX.XXX.com"; //username of oracle database
String pwd = "XXX"; //password of oracle database
Connection con = null; //connection variable assigned to null
try
{
Class.forName(driver);// for loading the jdbc driver
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("JDBC Driver loaded");
try
{
con=DriverManager.getConnection(cs,user,pwd);// for establishing connection with database
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Connected to the Oracle Database");
try
{
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}//end of main()
}//end of class()

rajkumar said...

Thanks very much friend !

Anonymous said...

i got this output:

JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Connected to the Oracle Database
java.lang.NullPointerException

what is this mean?

Anonymous said...

THANK for helping ....... really it is nice post ...
it help me thanks again

Anonymous said...

just copy classes 12.jar , it's enough.... And what about the type ii,iii and iv????

Anonymous said...

I got the below error...
Pls help

JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA
Connected to the Oracle Database
java.lang.NullPointerException

kavita said...

i got an error while connecting oracle to java pls help me...
error:

java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12514: TNS
:listener could not resolve SERVICE_NAME given in connect descriptor

selva said...

C:\Program Files\Java\jdk1.6.0_21\bin>java Conoracle
JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS
:could not resolve the connect identifier specified
Connected to the Oracle Database
java.lang.NullPointerException


plz help me i got this error while executing ur code

selvapriya said...

i got this output while executing ur code
plz help me please please

C:\Program Files\Java\jdk1.6.0_21\bin>javac Conoracle.java

C:\Program Files\Java\jdk1.6.0_21\bin>java Conoracle
JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS
:could not resolve the connect identifier specified
Connected to the Oracle Database
java.lang.NullPointerException

Anonymous said...

Exception in thread??/

Anonymous said...

Exception in thread "main" java.lang.NoClassDefError : connectOracle/Java
please help me!!!!

Anonymous said...

I configure ODBC as you said with username =scott. My data base user name is "scott". I typed same program given by you but changed username="scott" & password="tiger". every thing I kept same but the following error has come. Please help me my email:eplsofindia@yahoo.co.in
OUTPUT:
JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12541: TNS:no listener
Connected to the Oracle Database
java.lang.NullPointerException

Anonymous said...

help me out!!!
this is the o/p i got for sample program above!!!
i'm using windows vista sp2, jdk 1.6, oracle database 10g(10.2.0.3)
my mail id is santosh.bg@india.com

JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12541: TNS
:no listener
Connected to the Oracle Database
java.lang.NullPointerException

astika said...

lay Bhari,
Too Nice, Nice help for new comers.

Anonymous said...

Hello sir,
i got this output while executing ur code
plz help me please please



JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]Error while trying to retrieve text for error ORA-01019
Connected to the Oracle Database
java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 1 second)

Anonymous said...

i have same problem...plz help mee

Smarty said...

Simple and helpful

Javin @ ejb interview questions said...

You can also connect to Oracle by using type 4 driver, just add jdbc_6g.jar and you are ready. See this Java tutorial to connect to Oracle for step by step details.

Ramkumar said...

Please tell the solution for recover from this error

java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS:could not resolve the connect identifier specified
java.lang.NullPointerException

Anonymous said...

[Microsoft][ODBC driver for Oracle][Oracle]ORA-12224: TNS: no listener

xoxo said...

C:\Program Files\Java\jdk1.6.0_21\bin>java Conoracle
JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS
:could not resolve the connect identifier specified
Connected to the Oracle Database
java.lang.NullPointerException



Read more: http://bishwajeet.blogspot.com/2009/08/how-to-connect-java-application-with.html#ixzz241Kue5pi
Under Creative Commons License: Attribution Non-Commercial No Derivatives

xoxo said...

C:\Program Files\Java\jdk1.6.0_21\bin>java Conoracle
JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS
:could not resolve the connect identifier specified
Connected to the Oracle Database
java.lang.NullPointerException


wat does that mean???

Unknown said...

i want to connect jdbc to my program that is about user name password only.......
please suggest me...how to make this
on my e mail....dl8143@gmail.com

Anonymous said...

how to do connectivity between oracle and java in star uml

Anonymous said...

its showing a java language null pointer exception....any solutions.?

Unknown said...

The most effective method to Solve Oracle ORA-01019 Error Message through Remote DBA Services
The Oracle ORA-01019 blunder message implies unfit to distribute the memory in client side. The fundamental driver for this issue is the client side memory allocator returned mistake. Well! To tackle this issue you need to expand the procedures pile estimate or likewise you need to change to old arrangement of calls. Be that as it may, on the off chance that you need snappy arrangement with respect to this issue at that point rapidly contact to Database Administration for Oracle or Online Oracle DB Support.
For More Info: https://cognegicsystems.com/
Contact Number: 1-800-450-8670
Email Address- info@cognegicsystems.com
Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

EchoCyberPirate404 said...

Mardin
istanbul
Çanakkale
Antep
Elazığ
Q364FG

BAF2CRidgeF9B08 said...

E96AD
Eryaman Parke Ustası
Niğde Şehirler Arası Nakliyat
Erzincan Şehir İçi Nakliyat
Osmaniye Şehirler Arası Nakliyat
Van Lojistik
İzmir Parça Eşya Taşıma
Bitfinex Güvenilir mi
Balıkesir Şehirler Arası Nakliyat
Antep Şehirler Arası Nakliyat

E646EEsperanzaABB8D said...

9BEB5
sustanon
Sinop Evden Eve Nakliyat
Tokat Evden Eve Nakliyat
Sivas Evden Eve Nakliyat
Burdur Evden Eve Nakliyat
order sustanon
MuÄŸla Evden Eve Nakliyat
Balıkesir Evden Eve Nakliyat
Tekirdağ Boya Ustası

Post a Comment

Type here your comments