Wednesday, October 23, 2013

Export CSV with french or UTF-8 in Linux and Excel open it correctly with UTF-8

You may append the 3 bytes to the file before exporting, it works for me . Before doing that system only work in Windows and HP -UX but failed in Linux.

    FileOutputStream fStream = new FileOutputStream( f );
    final byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
    OutputStreamWriter writer = new OutputStreamWriter( fStream, "UTF8" );
    fStream.write( bom );
    // writer to have your contents

Have a UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file. Otherwise Excel will interpret the data according to your locale's default encoding (e.g. cp1252) instead of utf-8

Friday, April 19, 2013


Dynamically Clean up the target Oracle DB

  1. This job will call a main script. This script will first call a pre-defiend SQL file by replacing the proper DB USERNAME.
    SPOOL /home/tmp/drop.sql
    select 'drop table '||owner ||'.'||table_name||' cascade constraint;' as LBL_SCRIPT from all_tables where owner = upper('#_dbuser_#');
    select 'drop sequence '||sequence_owner||'.'||sequence_name||';' as LBL_SCRIPT from all_sequences where sequence_owner = upper('#_dbuser_#');
    select 'drop PROCEDURE '||object_name||';' as LBL_SCRIPT from ALL_OBJECTS where OWNER = upper('#_dbuser_#') and OBJECT_TYPE IN ('PROCEDURE');
    select 'drop FUNCTION '||object_name||';' as LBL_SCRIPT from ALL_OBJECTS where OWNER = upper('#_dbuser_#') and OBJECT_TYPE IN ('FUNCTION');
    SPOOL OFF
    exit
  2. The script will then try to select out the table,sequence,procedure and function from the target DB and paste it into a sql file by executing the SQL file in previous step.
    #########################################
    #  generate the drop script for existing db
    #########################################
    function generate_drop_script
    {
        echo "generate the drop script"
        sqlplus $dbuser/$dbpassword@ORCL @$gropFile
        echo "remove unwanted lines int the drop script"
        sed -i -e 's/[0-9]*.*row.*//ig' $finalDropFile
        sed -i -e 's/LBL_SCRIPT//g' $finalDropFile
        echo "append exit command to the drop script"
        echo "exit;" >> $finalDropFile
    }
  3. The script will remove those unwanted characters/words from the generated sql file in previous step.
  4. Execute the generated sql file to clean up the DB.

Remotely run the dump file for Oracle

When we have TWO servers
  1. server 1 (client server )
    • Platform - Linux Ret Hat
    • Oracle - Client
  2. server 2 (host server - the target server host the DB) - Linux Ret Hat
    • Platform - Linux Ret Hat
    • Oracle - Full Version
The following are the suggested steps to achieve this.
  1. Ensure the client server's Oracle Client is Administrator Type. If not, please do re-run the runInstaller
  2. This to ensure the Oracle Client provide 'import' and 'export' services.
  3. Add the host server IP into client server's
    • File name - host.allow
      • E.g: /etc/host.allow
      • Line to be added -> nfs: 10.116.1.123
    • File name - hosts
      • E.g: /etc/hosts
      • 10.116.1.123 ORCL dbmain.company.com
  4. Both server need to start the following services
    • portmap
    • netfs
    • nfs
    • E.g : $ /sbin/service nfs start
  5. Add the following line into file etc/exports
    /home linux(rw,sync) linuxd600(rw,sync)
    /u01 10.116.1.123(rw,sync)
  6. Must define the database address for establishing connection instead of pure text such as :-
    $ imp host_user/host.com@'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST='$DB_HOST')(PORT='$DB_PORT')))(CONNECT_DATA=(SID='$DB_SID')))' FILE=/get/dump/file/directory/dbdump.dmp FULL=y
    • Please define it in tnsnames.ora file E.g: /u01/app/oracle/product/11.2.0/client_1/network/admin/tnsnames.ora
      ORCL=
        (DESCRIPTION =
          (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = dbmain.company.com)(PORT = 1521))
          )
          (CONNECT_DATA =
            (SID = orcl)
          )
        )
    • No need to restart listener as Oracle Client do not have such services being installed.
  7. Set the proper environment variables
    $ export ORACLE_HOME=/u01/app/oracle/product/11.2.0/client_1
    $ PATH=$PATH:$ORACLE_HOME/bin
    $ LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib; export LD_LIBRARY_PATH
    $ export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
  8. Now is the time to execute the dump file.
    $ imp cnm_developerX/password@ORCL FILE=/get/dump/file/directory/dbdump.dmp FULL=y