Monday, August 17, 2009

How to insert data into oracle database using java program

Oracle CorporationImage via Wikipedia

Hey friends I am here to help you. In this tutorial we will see how to insert data into oracle data base using java program/application.

Prerequisites:
  1. All configuration must me done to connect java

    Java (programming language)Image via Wikipedia

    application to oracle before starting this task. As described in my previous tutorial 'How to connect java application with oracle database'.
Steps:
  1. First start your oracle database and create a table of your choice. eg - create table student (id number(3), name varchar2(30),class number(2),marks number(3));
  2. Now open IDE and write down the java code for the application which will connect to the oracle database and enter the data to the specific table.
  3. After compilation and execution. You will get data inserted in the database.
Source Code for 'Inserting data into oracle database using java program/application':

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class InsertRecord {

public static void main(String[] args) {

String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String cs="jdbc:odbc:connect_oracle";
String user = "system";
String pwd = "tom";
String sqlstmt="INSERT INTO STUDENT VALUES(1,'Steve',5,70)";
Connection con = null;
Statement st = null;
try
{
Class.forName(driver);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Driver loaded");
try
{
con=DriverManager.getConnection(cs,user,pwd);
System.out.println("Connected to the Oracle Database");
st = con.createStatement();//creates a Statement object for sending SQL statements to the database.
int updatecount=st.executeUpdate(sqlstmt);//return either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing
System.out.println(updatecount+" row inserted");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
st.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}//main()
}//class()

Output:

Driver loaded
Connected to the Oracle Database
1 record inserted


In my next post I will discuss about how to add interactively multiple records to the oracle database. Till then have a good time.

Reblog this post [with Zemanta]


Bookmark and Share

27 comments:

Anonymous said...

thanks man it was very helpful for me.....atleast I cud understand where I was making a mistake....hats off to U......cheers..
Swapnil Walke

Anonymous said...

hi plz . tell how to insert values at run time?

Anonymous said...

thanku very much...god bless you

Anonymous said...

hiii

please help me out i am not able to connect to Data Base: i get an Exception like:
ORA-12514:TNS:listner could not resolve SERVICE_NAME given in connect desriptor

DataSourceName: Oracle
Platform:Windows XP

my code:
public static void main(String[] args) {

String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String cs="jdbc:odbc:Oracle";
String user = "scott";
String pwd = "tiger";
String sqlstmt="INSERT INTO STUDENT VALUES(2,'Steve',50)";
Connection con = null;
Statement st = null;
try
{
Class.forName(driver);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Driver loaded");
try
{
con=DriverManager.getConnection(cs,user,pwd);
System.out.println("Connected to the Oracle Database");
st = con.createStatement();//creates a Statement object for sending SQL statements to the database.
int updatecount=st.executeUpdate(sqlstmt);//return either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing
System.out.println(updatecount+" row inserted");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
st.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}//main()
}//class()

please let me know!! please reply

Unknown said...

thank u for giving explanation very gud explanation

Anonymous said...

excellent post

govardhan said...

sir,
am govardhan while an trying to connect am getting this error plz help me
am using oracle 10 g
usr name is scott and password is tiger
plz forword ans to my mail id plz plz
my mail id is (govardhandevari@gmail.com)
C:\Documents and Settings\DIGIT\Desktop>java Test1 Exception in thread "main" java.lang.UnsupportedClassVersionError: Test1 (Unsupported major.minor version 50.0) at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source)

selvapriya said...

plz help me how to connect java and oracle 10g

selvapriya said...

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

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

selvapriya said...

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

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



i got the above error while running ur code so please guide me to connect to oracle database from java. Actually i am using oracle10g, jdk1.6.0_21

Anonymous said...

thank u so much..............

Anonymous said...

done a great job.............it s usefulll for begineers.......as like me........

Anonymous said...

this is very easy way of jdbc with oracle
thanks sir
not any error in this program

Anonymous said...

JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-06413: Connection not open.
Connected to the Oracle Database
java.lang.NullPointerException
JDBC Driver loaded
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-06413: Connection not open.
Connected to the Oracle Database
java.lang.NullPointerException


this is the error i am getting plz help me

Anonymous said...

hey i am getting an error
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
i have copied ojdbc14.jar in my lib folder but still it is giving the same exception
i tried it on eclipse also and include external jar but getting the same error ...pls tell me how to resolve this...

İlkben5 said...

Ankara
Antalya
istanbul
Ordu
izmir
Z1FMR1

İkbal said...

izmir
Erzurum
Diyarbakır
Tekirdağ
Ankara
5HRVEV

Kasım21 said...

yozgat
sivas
bayburt
van
uşak
70E

1BD3BSaniyah2E3BD said...

02361
Gümüşhane Evden Eve Nakliyat
Niğde Evden Eve Nakliyat
Muğla Evden Eve Nakliyat
Kırıkkale Parça Eşya Taşıma
Ankara Evden Eve Nakliyat

3C5FAFaithCBE3E said...

0BCA7
Bayburt Şehir İçi Nakliyat
Bitlis Şehirler Arası Nakliyat
Ankara Boya Ustası
Ünye Marangoz
Denizli Parça Eşya Taşıma
Çerkezköy Petek Temizleme
Mardin Lojistik
İstanbul Lojistik
Edirne Şehir İçi Nakliyat

5878AGene42AC0 said...

2617E
Maraş Lojistik
Etlik Boya Ustası
Shibanomi Coin Hangi Borsada
Hakkari Şehir İçi Nakliyat
Erzincan Parça Eşya Taşıma
Gölbaşı Fayans Ustası
Tekirdağ Şehir İçi Nakliyat
Ankara Evden Eve Nakliyat
Uşak Lojistik

B3A3ACiara6AF0F said...

39480
Tunceli Parça Eşya Taşıma
Sivas Evden Eve Nakliyat
Karaman Evden Eve Nakliyat
Tokat Lojistik
Nevşehir Lojistik
Kastamonu Parça Eşya Taşıma
İzmir Şehirler Arası Nakliyat
Amasya Şehirler Arası Nakliyat
Çerkezköy Çilingir

EA02CLeaBFCA6 said...

B2B85
adana canli sohbet chat
malatya yabancı görüntülü sohbet
mersin kadınlarla sohbet
Kilis Kızlarla Rastgele Sohbet
osmaniye canli sohbet chat
Balıkesir Bedava Sohbet Uygulamaları
yabancı görüntülü sohbet siteleri
ısparta Telefonda Görüntülü Sohbet
karabük canli sohbet bedava

71EE7Allison528E8 said...

5732B
kırşehir yabancı görüntülü sohbet siteleri
canli sohbet
Uşak Muhabbet Sohbet
kastamonu rastgele görüntülü sohbet uygulaması
Tokat Rastgele Sohbet Siteleri
çanakkale sohbet siteleri
canlı sohbet ücretsiz
Osmaniye Telefonda Kadınlarla Sohbet
Tekirdağ En İyi Rastgele Görüntülü Sohbet

DB97AMerrick3C17C said...

BEB00
ledger live
poocoin
looksrare
arculus
metamask
phantom
poocoin
safepal
defillama

DED85Audrey51D53 said...

67385
uwulend finance
DefiLlama
dexview
thorchain
spookyswap
satoshivm
defillama
pancakeswap
dappradar

70BDD229E5Nina71BE5C034B said...

2B5DCD2057
steroid satın al
görüntülü show
cialis
steroid satın al
www.ijuntaxmedikal.store
şov

Post a Comment

Type here your comments