/*

 * This sample shows how to insert data in a table.

 */

 

// You need to import the java.sql package to use JDBC

import java.sql.*;

 

class InsertExample

{

  public static void main (String args [])

       throws SQLException

  {

    // Load the Oracle JDBC driver

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

 

    String url = "jdbc:oracle:oci8:@";

    try {

      String url1 = System.getProperty("JDBC_URL");

      if (url1 != null)

        url = url1;

    } catch (Exception e) {

      // If there is any security exception, ignore it

    }

 

    // Connect to the database

    Connection conn =

      DriverManager.getConnection (url, "scott", "tiger");

 

 

    // Prepare a statement to cleanup the emp table

    Statement stmt = conn.createStatement ();

    try

    {

      stmt.execute ("delete from EMP where EMPNO = 1500");

    }

    catch (SQLException e)

    {

      // Ignore an error here

    }

 

    try

    {

      stmt.execute ("delete from EMP where EMPNO = 507");

    }

    catch (SQLException e)

    {

      // Ignore an error here too

    }

 

    // Close the statement

    stmt.close();

 

    // Prepare to insert new names in the EMP table

    PreparedStatement pstmt =

      conn.prepareStatement ("insert into EMP (EMPNO, ENAME) values (?, ?)");

 

    // Add LESLIE as employee number 1500

    pstmt.setInt (1, 1500);          // The first ? is for EMPNO

    pstmt.setString (2, "LESLIE");   // The second ? is for ENAME

    // Do the insertion

    pstmt.execute ();

 

    // Close the statement

    pstmt.close();

 

    // Close the connecion

    conn.close();

 

  }

}