Making Changes to the Database


To write changes to the database, such as for INSERT or UPDATE operations, you will typically create a PreparedStatement object. Instead of calling the same statement over and over with different inputs, you can instead use a PreparedStatement, which allows you to execute a statement with varying sets of input parameters. The java.sql.PreparedStatement interface allows binding parameters which are associating a parameter (indicated by a placeholder like ? in the prepared statement) with an actual Java value. Use setXXX( ) methods on the PreparedStatement object to bind data into the prepared statement to be sent to the database. The following example shows how to use a prepared statement to execute INSERT operations that add rows to the emp_tbl table:

 SQL> CREATE TABLE  emp_tbl (
   2    empno  INTEGER      PRIMARY KEY,
   3    ename  VARCHAR(64)  NOT NULL );
 Table created.

 SQL> CREATE SEQUENCE  empid_seq  MINVALUE 1
   2    START WITH 1 INCREMENT BY 1 NOCACHE;
 Sequence created.

 SQL> INSERT INTO  emp_tbl
   2    SELECT  empid_seq.NEXTVAL, 'LESLIE'  FROM DUAL;
 1 row created.

 SQL> INSERT INTO  emp_tbl  VALUES( 507, 'ANNETTE' );
 1 row created.

Name1 (args[0]) =

Name2 (args[1]) =

Name3 (args[2]) =

                       
// Import the following packages to use JDBC.
import  java.sql.*;
import  java.io.*;
import  oracle.sql.*;
import  oracle.jdbc.*;
import  oracle.jdbc.pool.OracleDataSource;

class  MakeChanges {
  public static void  main( String args[ ] ) throws SQLException {
    String user     = "C##userid";
    String password = "password";
    String database = "65.52.222.73:1521/cdb1";

    // Open an OracleDataSource and get a connection.
    OracleDataSource ods = new OracleDataSource( );
    ods.setURL     ( "jdbc:oracle:thin:@" + database );
    ods.setUser    ( user );
    ods.setPassword( password );
    Connection conn = ods.getConnection( );

    try {
      Statement  stmt = conn.createStatement( );
      // Clear the table EMP_TBL.
      stmt.executeUpdate( "DELETE FROM emp_tbl" );

      // Prepare to insert two employees in the EMP_TBL table and
      // use the create sequence for the EMPNO of the employees.
      String  cmd  = "INSERT INTO emp_tbl ";
              cmd += "SELECT empid_seq.NEXTVAL, ? FROM DUAL";
      PreparedStatement  pstmt = conn.prepareStatement( cmd );

      // Add the first employee and the first ? is for ENAME.
      pstmt.setString( 1, args[0].trim( ) );
      // Do the insertion.
      pstmt.execute( );
      // Add the second employee and the first ? is for ENAME.
      pstmt.setString( 1, args[1].trim( ) );
      pstmt.execute  ( );

      // Add the third employee with an employee number 507.
      // Prepare to insert new names in the EMP_TBL table.
      cmd  = "INSERT INTO EMP_TBL( EMPNO, ENAME ) VALUES( ?, ? )";
      pstmt = conn.prepareStatement( cmd );

      pstmt.setInt   ( 1, 507 );        // The first ? is for EMPNO
      pstmt.setString( 2, args[2].trim( ) );   // The second ? is for ENAME
      pstmt.execute  ( );

      // Close the statement.
      pstmt.close( );

      ResultSet  rs = stmt.executeQuery( "SELECT * FROM emp_tbl" );
      while ( rs.next( ) ) {
        System.out.print( "empno: " + rs.getInt( 1 )   + "      " );
        System.out.print( "ename: " + rs.getString( 2 ) );
      }
      rs.close  ( );
      stmt.close( );
    }
    catch( SQLException e ) { System.out.println( e ); }
    conn.close( );
  }
}