// 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##user_id";
    String password = "password";
    String database = "20.185.147.112:1521/xe";
    // 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( );
  }
}
   |