FileDocCategorySizeDatePackage
InsertExample.javaAPI DocExample1550Wed Sep 17 13:50:08 BST 1997None

InsertExample

public class InsertExample extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    // Load the Oracle JDBC driver
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    // Connect to the database
    // You can put a database name after the @ sign in the connection URL.
    Connection conn =
      DriverManager.getConnection ("jdbc:oracle:oci7:@", "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
    }

    // 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 ();

    // Add MARSHA as employee number 507
    pstmt.setInt (1, 507);           // The first ? is for EMPNO
    pstmt.setString (2, "MARSHA");   // The second ? is for ENAME
    // Do the insertion
    pstmt.execute ();