473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to improve my java class performance while selecting and inserting

36
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.setAutoCommit(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 preparedstatement ...
Check this once and send some of the performance improvement steps ...

Expand|Select|Wrap|Line Numbers
  1.  import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Enumeration;
  8. import weblogic.jdbc.vendor.oracle.OracleArray;
  9. import atg.security.MD5PasswordHasher;
  10. import com.objectspace.jgl.HashMap;
  11.  
  12. public class PasswordMigration1 {
  13.  
  14.     /**
  15.      * @param args
  16.      */
  17.     static String userId;
  18.     static String asisPassword;
  19.  
  20.     public static void main(String[] args) {
  21.         // TODO Auto-generated method stub
  22.         try {
  23.             System.out.println("Password Migration Starts At time:"+System.currentTimeMillis());
  24.             DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
  25.              Connection conn = DriverManager.getConnection(
  26.           "jdbc:oracle:oci:@(description=(address=(host=172.20.232.35)" +
  27.             "(protocol=tcp)(port=1521))(CONNECT_DATA=(SERVICE_NAME=CVSDMGN)))",
  28.           "atgcore_o",
  29.           "atgcore_o"
  30.         );
  31.              conn.setAutoCommit(false); 
  32.               PreparedStatement stmt = conn.prepareStatement("select tmig_migrate_profile.mbr_nbr, password from Member, tmig_migrate_profile where Member.mbr_nbr=tmig_migrate_profile.mbr_nbr");
  33.               ResultSet rset = stmt.executeQuery();
  34.               String sqlQuery;
  35.               Statement stmtMigrate=conn.createStatement();
  36.               String sqlQueryAsis;
  37.               Statement stmtAsis=conn.createStatement();
  38.  
  39.               int count=0;
  40.               long size=793336;
  41.                 while (rset.next())
  42.                 {
  43.                     count++;
  44.                     userId=rset.getString(1);
  45.                     asisPassword=rset.getString(2);
  46.                     String decryptedPassword=decryptPassword(asisPassword);
  47.                     String encryptedPassword=encryptPassword(decryptedPassword);
  48.                     sqlQuery="INSERT INTO TMIG_USER_PWD (user_id,password) VALUES(" +
  49.                     "'"+userId+"','"+encryptedPassword+"'"+")";
  50.                     stmtMigrate.addBatch(sqlQuery);
  51.                      sqlQueryAsis="INSERT INTO TMIG_USER_PWD_ASIS (user_id,password) VALUES(" +
  52.                       "'"+userId+"','"+decryptedPassword+"'"+")";
  53.                     stmtAsis.addBatch(sqlQueryAsis);
  54.                      if(count%100==0 || ((size-count)==0 ) ){
  55.                         stmtMigrate.executeBatch();
  56.                         stmtAsis.executeBatch();
  57.                         stmtMigrate.clearBatch();
  58.                         stmtAsis.clearBatch();
  59.                         count=0;
  60.                         size=size-100;
  61.                     }
  62.                 }
  63.                 stmtMigrate.close();
  64.                 stmtAsis.close();
  65.                 stmt.close();
  66.                 conn.close();
  67.                 System.out.println("Password Migration ENDS::"+System.currentTimeMillis());
  68.         } catch (SQLException e) {
  69.             // TODO Auto-generated catch block
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.     private static String encryptPassword(String decryptedPassword) {
  74.         // TODO Auto-generated method stub
  75.             MD5PasswordHasher passwordHasher=new MD5PasswordHasher();
  76.             return passwordHasher.encryptPassword(decryptedPassword);        
  77.     }
  78.     private static String decryptPassword(String asisPassword) {
  79.         // TODO Auto-generated method stub
  80.         int length = asisPassword.length();
  81.         int temp=2;
  82.         char c;
  83.         StringBuffer sb = new StringBuffer();
  84.         for(int i=0;i<asisPassword.length();i=i+2)
  85.         {
  86.             String StringToken = (asisPassword.substring(i,temp));
  87.             int intToHex = Integer.parseInt(StringToken, 16);
  88.             c = (char) intToHex;
  89.             sb.append(c);
  90.             temp=((temp==length-1)?length:(temp+2));
  91.         }
  92.  
  93.         return sb.toString();
  94.     }
  95. }
Mar 13 '08 #1
2 2754
r035198x
13,262 8TB
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?
Mar 13 '08 #2
sukatoa
539 512MB
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.setAutoCommit(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 preparedstatement ...
Check this once and send some of the performance improvement steps ...

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import weblogic.jdbc.vendor.oracle.OracleArray;
import atg.security.MD5PasswordHasher;
import com.objectspace.jgl.HashMap;

public class PasswordMigration1 {

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

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println("Password Migration Starts At time:"+System.currentTimeMillis());
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:oci:@(description=(address=(host=172. 20.232.35)" +
"(protocol=tcp)(port=1521))(CONNECT_DATA=(SERVICE_ NAME=CVSDMGN)))",
"atgcore_o",
"atgcore_o"
);
conn.setAutoCommit(false);
PreparedStatement stmt = conn.prepareStatement("select tmig_migrate_profile.mbr_nbr, password from Member, tmig_migrate_profile where Member.mbr_nbr=tmig_migrate_profile.mbr_nbr");
ResultSet rset = stmt.executeQuery();
String sqlQuery;
Statement stmtMigrate=conn.createStatement();
String sqlQueryAsis;
Statement stmtAsis=conn.createStatement();

int count=0;
long size=793336;
while (rset.next())
{
count++;
userId=rset.getString(1);
asisPassword=rset.getString(2);
String decryptedPassword=decryptPassword(asisPassword);
String encryptedPassword=encryptPassword(decryptedPasswor d);
sqlQuery="INSERT INTO TMIG_USER_PWD (user_id,password) VALUES(" +
"'"+userId+"','"+encryptedPassword+"'"+")";
stmtMigrate.addBatch(sqlQuery);
sqlQueryAsis="INSERT INTO TMIG_USER_PWD_ASIS (user_id,password) VALUES(" +
"'"+userId+"','"+decryptedPassword+"'"+")";
stmtAsis.addBatch(sqlQueryAsis);
if(count%100==0 || ((size-count)==0 ) ){
stmtMigrate.executeBatch();
stmtAsis.executeBatch();
stmtMigrate.clearBatch();
stmtAsis.clearBatch();
count=0;
size=size-100;
}
}
stmtMigrate.close();
stmtAsis.close();
stmt.close();
conn.close();
System.out.println("Password Migration ENDS::"+System.currentTimeMillis());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String encryptPassword(String decryptedPassword) {
// TODO Auto-generated method stub
MD5PasswordHasher passwordHasher=new MD5PasswordHasher();
return passwordHasher.encryptPassword(decryptedPassword);
}
private static String decryptPassword(String asisPassword) {
// TODO Auto-generated method stub
int length = asisPassword.length();
int temp=2;
char c;
StringBuffer sb = new StringBuffer();
for(int i=0;i<asisPassword.length();i=i+2)
{
String StringToken = (asisPassword.substring(i,temp));
int intToHex = Integer.parseInt(StringToken, 16);
c = (char) intToHex;
sb.append(c);
temp=((temp==length-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
Mar 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Leader | last post by:
Hi All, I am getting slower performance of select statements in MS SQL. I am finding select statements in MS SQL are even slower than MS ACCESS. Is there any way to improve the performance of...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
20
by: Scott M. | last post by:
What are the advantages of defining a class as part of another class definition (nesting classes)?
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: Swami | last post by:
I have 2 questions relating to website design in asp .net: 1. In a website that I am building I have everything as a user control. Even the header, which contains the navigation tabs is in a user...
1
by: 848lu | last post by:
hey i really need help...i got this code....basically im suppose to make a calender that allows a user to type in month and year .... and the calander displays it on the scree using...
5
by: smileskhan | last post by:
Hay Friends... Here I started a new and interested thread. I hope you also enjoy it. I got a task to creat a Hospital Database in Java. But I donīt have any good clues abouts it....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.