Insert record into Oracle - Java JDBC

Java examples for JDBC:Oracle

Description

Insert record into Oracle

Demo Code

//import java.sql.DriverManager;
import java.sql.*;

import java.util.*;
//import oracle.jdbc.OracleConnection;
import oracle.jdbc.*;

class InserterRow1
{
    public InserterRow1()
    {//from   w  w  w  .  j a  va  2  s  .co m
        String vDEPTNO,vDNAME,vLOC;
        Scanner sc =new Scanner(System.in);
        System.out.println("Preparering for inserting a record into the table");
        System.out.print("Enter the vDeptno:");
        vDEPTNO=sc.next();
        System.out.print("Enter the vDNAME:");
        vDNAME=sc.next();
        System.out.print("Enter the LOC:");
        vLOC=sc.next();
        // 1.registering the java program with oracle....
        try
        {
            //The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class.
           Class.forName("oracle.jdbc.driver.OracleDriver"); 
           // 2. creating the address to locate the oracle database
           String url="jdbc:oracle:thin:@Islam-PC:1521:ORCL";
           // 3.creating and instantiating the connection object.. i.e connecting to the oracle database ....
           OracleConnection conn=(OracleConnection)DriverManager.getConnection(url,"ABDUS","SAMAD");  //The getConnection() method of DriverManager class is used to establish connection with the database.
           System.out.println("Connection with Oracle database estsblised successfully.......");
           // 4.creating and instantiating the PREPAREstatement object...
           PreparedStatement pstmt=conn.prepareStatement("INSERT INTO DEPT VALUES('"+vDEPTNO+"','"+vDNAME+"','"+vLOC+"')");  
           
           // 5.Executing the query ....
           int rowsAffected=pstmt.executeUpdate();
           System.out.println("No of rows inserted is : " + rowsAffected);
           //7. Closing the connections....
           pstmt.close();
           conn.close();
        }
        catch(ClassNotFoundException|SQLException ex)
        {
            System.out.println(ex.toString());
        }
        
        
    }
}
public class RecordInserter1 
{
    public static void main(String args[])
    {
        InserterRow1 ic1=new InserterRow1();
    }
}

Related Tutorials