how to improve my java class performance while selecting and inserting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sdanda
    New Member
    • Oct 2007
    • 36

    how to improve my java class performance while selecting and inserting

    Hi ,

    Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity .........
    This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known

    1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver
    2) In that programme I m using prepared statement instead of statement
    3) I am executing the statements as batches
    4) I am using this conn.setAutoCom mit(false); for improve my performance

    Whether we have any option to improve my performance still ?

    I am sending the code below,in that I have to change statement as preparedstateme nt ...
    Check this once and send some of the performance improvement steps ...

    [CODE=java] import java.sql.Connec tion;
    import java.sql.Driver Manager;
    import java.sql.Prepar edStatement;
    import java.sql.Result Set;
    import java.sql.SQLExc eption;
    import java.sql.Statem ent;
    import java.util.Enume ration;
    import weblogic.jdbc.v endor.oracle.Or acleArray;
    import atg.security.MD 5PasswordHasher ;
    import com.objectspace .jgl.HashMap;

    public class PasswordMigrati on1 {

    /**
    * @param args
    */
    static String userId;
    static String asisPassword;

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    System.out.prin tln("Password Migration Starts At time:"+System.c urrentTimeMilli s());
    DriverManager.r egisterDriver (new oracle.jdbc.dri ver.OracleDrive r());
    Connection conn = DriverManager.g etConnection(
    "jdbc:oracle:oc i:@(description =(address=(host =172.20.232.35) " +
    "(protocol=tcp) (port=1521))(CO NNECT_DATA=(SER VICE_NAME=CVSDM GN)))",
    "atgcore_o" ,
    "atgcore_o"
    );
    conn.setAutoCom mit(false);
    PreparedStateme nt stmt = conn.prepareSta tement("select tmig_migrate_pr ofile.mbr_nbr, password from Member, tmig_migrate_pr ofile where Member.mbr_nbr= tmig_migrate_pr ofile.mbr_nbr") ;
    ResultSet rset = stmt.executeQue ry();
    String sqlQuery;
    Statement stmtMigrate=con n.createStateme nt();
    String sqlQueryAsis;
    Statement stmtAsis=conn.c reateStatement( );

    int count=0;
    long size=793336;
    while (rset.next())
    {
    count++;
    userId=rset.get String(1);
    asisPassword=rs et.getString(2) ;
    String decryptedPasswo rd=decryptPassw ord(asisPasswor d);
    String encryptedPasswo rd=encryptPassw ord(decryptedPa ssword);
    sqlQuery="INSER T INTO TMIG_USER_PWD (user_id,passwo rd) VALUES(" +
    "'"+userId+"',' "+encryptedPass word+"'"+")";
    stmtMigrate.add Batch(sqlQuery) ;
    sqlQueryAsis="I NSERT INTO TMIG_USER_PWD_A SIS (user_id,passwo rd) VALUES(" +
    "'"+userId+"',' "+decryptedPass word+"'"+")";
    stmtAsis.addBat ch(sqlQueryAsis );
    if(count%100==0 || ((size-count)==0 ) ){
    stmtMigrate.exe cuteBatch();
    stmtAsis.execut eBatch();
    stmtMigrate.cle arBatch();
    stmtAsis.clearB atch();
    count=0;
    size=size-100;
    }
    }
    stmtMigrate.clo se();
    stmtAsis.close( );
    stmt.close();
    conn.close();
    System.out.prin tln("Password Migration ENDS::"+System. currentTimeMill is());
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTra ce();
    }
    }
    private static String encryptPassword (String decryptedPasswo rd) {
    // TODO Auto-generated method stub
    MD5PasswordHash er passwordHasher= new MD5PasswordHash er();
    return passwordHasher. encryptPassword (decryptedPassw ord);
    }
    private static String decryptPassword (String asisPassword) {
    // TODO Auto-generated method stub
    int length = asisPassword.le ngth();
    int temp=2;
    char c;
    StringBuffer sb = new StringBuffer();
    for(int i=0;i<asisPassw ord.length();i= i+2)
    {
    String StringToken = (asisPassword.s ubstring(i,temp ));
    int intToHex = Integer.parseIn t(StringToken, 16);
    c = (char) intToHex;
    sb.append(c);
    temp=((temp==le ngth-1)?length:(temp +2));
    }

    return sb.toString();
    }
    }[/CODE]
    Last edited by r035198x; Mar 13 '08, 12:12 PM. Reason: added code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use code tags when posting code. Posting code without code tags is against site rules.
    2.) Use StringBuilder instead of StringBuffer.
    3.) Don`t you have to call commit at some point if you`ve set autoCommit to false?

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by sdanda
      Hi ,

      Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity .........
      This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known

      1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver
      2) In that programme I m using prepared statement instead of statement
      3) I am executing the statements as batches
      4) I am using this conn.setAutoCom mit(false); for improve my performance

      Whether we have any option to improve my performance still ?

      I am sending the code below,in that I have to change statement as preparedstateme nt ...
      Check this once and send some of the performance improvement steps ...

      import java.sql.Connec tion;
      import java.sql.Driver Manager;
      import java.sql.Prepar edStatement;
      import java.sql.Result Set;
      import java.sql.SQLExc eption;
      import java.sql.Statem ent;
      import java.util.Enume ration;
      import weblogic.jdbc.v endor.oracle.Or acleArray;
      import atg.security.MD 5PasswordHasher ;
      import com.objectspace .jgl.HashMap;

      public class PasswordMigrati on1 {

      /**
      * @param args
      */
      static String userId;
      static String asisPassword;

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      try {
      System.out.prin tln("Password Migration Starts At time:"+System.c urrentTimeMilli s());
      DriverManager.r egisterDriver (new oracle.jdbc.dri ver.OracleDrive r());
      Connection conn = DriverManager.g etConnection(
      "jdbc:oracle:oc i:@(description =(address=(host =172.20.232.35) " +
      "(protocol=tcp) (port=1521))(CO NNECT_DATA=(SER VICE_NAME=CVSDM GN)))",
      "atgcore_o" ,
      "atgcore_o"
      );
      conn.setAutoCom mit(false);
      PreparedStateme nt stmt = conn.prepareSta tement("select tmig_migrate_pr ofile.mbr_nbr, password from Member, tmig_migrate_pr ofile where Member.mbr_nbr= tmig_migrate_pr ofile.mbr_nbr") ;
      ResultSet rset = stmt.executeQue ry();
      String sqlQuery;
      Statement stmtMigrate=con n.createStateme nt();
      String sqlQueryAsis;
      Statement stmtAsis=conn.c reateStatement( );

      int count=0;
      long size=793336;
      while (rset.next())
      {
      count++;
      userId=rset.get String(1);
      asisPassword=rs et.getString(2) ;
      String decryptedPasswo rd=decryptPassw ord(asisPasswor d);
      String encryptedPasswo rd=encryptPassw ord(decryptedPa ssword);
      sqlQuery="INSER T INTO TMIG_USER_PWD (user_id,passwo rd) VALUES(" +
      "'"+userId+"',' "+encryptedPass word+"'"+")";
      stmtMigrate.add Batch(sqlQuery) ;
      sqlQueryAsis="I NSERT INTO TMIG_USER_PWD_A SIS (user_id,passwo rd) VALUES(" +
      "'"+userId+"',' "+decryptedPass word+"'"+")";
      stmtAsis.addBat ch(sqlQueryAsis );
      if(count%100==0 || ((size-count)==0 ) ){
      stmtMigrate.exe cuteBatch();
      stmtAsis.execut eBatch();
      stmtMigrate.cle arBatch();
      stmtAsis.clearB atch();
      count=0;
      size=size-100;
      }
      }
      stmtMigrate.clo se();
      stmtAsis.close( );
      stmt.close();
      conn.close();
      System.out.prin tln("Password Migration ENDS::"+System. currentTimeMill is());
      } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTra ce();
      }
      }
      private static String encryptPassword (String decryptedPasswo rd) {
      // TODO Auto-generated method stub
      MD5PasswordHash er passwordHasher= new MD5PasswordHash er();
      return passwordHasher. encryptPassword (decryptedPassw ord);
      }
      private static String decryptPassword (String asisPassword) {
      // TODO Auto-generated method stub
      int length = asisPassword.le ngth();
      int temp=2;
      char c;
      StringBuffer sb = new StringBuffer();
      for(int i=0;i<asisPassw ord.length();i= i+2)
      {
      String StringToken = (asisPassword.s ubstring(i,temp ));
      int intToHex = Integer.parseIn t(StringToken, 16);
      c = (char) intToHex;
      sb.append(c);
      temp=((temp==le ngth-1)?length:(temp +2));
      }

      return sb.toString();
      }
      }
      About your while loop and for loop

      Comparison to 0 is faster than comparisons to most other numbers. The VM has
      optimizations for comparisons to the integers -1, 0, 1, 2, 3, 4, and 5. So rewriting loops to
      make the test a comparison against may be faster.[1] This alteration typically reverses the
      iteration order of the loop from counting up (0 to max) to counting down (max to 0).

      That's all i can share....
      Sukatoa

      Comment

      Working...