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]

34 comments:

  1. 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

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

    ReplyDelete
  3. thanku very much...god bless you

    ReplyDelete
  4. 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

    ReplyDelete
  5. thank u for giving explanation very gud explanation

    ReplyDelete
  6. 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)

    ReplyDelete
  7. plz help me how to connect java and oracle 10g

    ReplyDelete
  8. 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

    ReplyDelete
  9. 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

    ReplyDelete
  10. thank u so much..............

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

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

    ReplyDelete
  13. 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

    ReplyDelete
  14. 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...

    ReplyDelete
  15. E8663EA55BSebastian334A4D1BBFDecember 29, 2024 at 3:33 PM

    14095982AD
    tiktok takipçi

    ReplyDelete

Type here your comments