By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,156 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,156 IT Pros & Developers. It's quick & easy.

How to overwrite existing records

madhoriya22
100+
P: 252
Hi,
I am getting the data from the CSV file and inserting it to the database. Now while inserting I have to check that some of the data(to be inserted) is already existing in the table or not. If existing then I have to overwrite that data. Now here is what I have done :--

My table has some fields like Id(primary key), name, and imported_date. Now what I am doing is I am comparing all the Ids(in the table) and Ids(in the CSV file. like this:-
Expand|Select|Wrap|Line Numbers
  1.  
  2. rs = pStatement.executeQuery();
  3.  
  4. while(rs.next()) {
  5. for(int i = 0; i < v.size(); i++) {
  6. if(((DefectDetailVO)v.get(i)).getDefectId().equals(rs.getString("DEFECT_ID"))) {
  7. System.out.println("Is Record Updated::: " +updateExistingDefectDetails((DefectDetailVO)v.get(i)));
  8. v.removeElementAt(i);//Here v is a vector and consists records coming from CSV file
  9. break;
  10. }
  11. }
  12.  
This logic is working fine. But I want to know Is there any other more optimize way to do that. Because as the records will increase in the table this section(code) could decrease the code performance.
Aug 9 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 10K+
P: 11,448
Why don't you turn the logic around the other way? i.e. try to executeUpdate()
all the data from your CSV data set. If the update succeeds remove the data
from your vector. At the end the vector just contains the elements that were *not*
in the database yet; insert them. This way no resultset needs to be build for
every row in the CSV set.

kind regards,

Jos
Aug 9 '07 #2

madhoriya22
100+
P: 252
Why don't you turn the logic around the other way? i.e. try to executeUpdate() all the data from your CSV data set. If the update succeeds remove the data from your vector. At the end the vector just contains the elements that were *not* in the database yet; insert them. This way no resultset needs to be build for every row in the CSV set.

kind regards,

Jos
Hi Jos,
Tried ur way. I think it is working but the only problem is if the vector consists records that are all preexisted then while updating it leaves one record in the vector(according to condition it should be 0). Here is what I have done:-
Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i = 0; i < v.size(); i++) {
  3.                 pStatement.setString(1, ((DefectDetailVO)v.get(i)).getSeverity());
  4.                 pStatement.setString(2, ((DefectDetailVO)v.get(i)).getPriority());
  5.                 pStatement.setString(3, ((DefectDetailVO)v.get(i)).getAssignee());
  6.                 pStatement.setString(4, ((DefectDetailVO)v.get(i)).getStatus());
  7.                 pStatement.setString(5, ((DefectDetailVO)v.get(i)).getCurrentDefectStatus());
  8.                 pStatement.setString(6, ((DefectDetailVO)v.get(i)).getPhase());
  9.                 pStatement.setString(7, ((DefectDetailVO)v.get(i)).getTargetMilestone());
  10.                 pStatement.setString(8, ((DefectDetailVO)v.get(i)).getKeywords());
  11.                 pStatement.setString(9, ((DefectDetailVO)v.get(i)).getDefectId());
  12.  
  13.                 int status = pStatement.executeUpdate();
  14.  
  15.                 if(status >0) {
  16.                     System.out.println("Updated " +i);
  17.                     v.removeElementAt(i);
  18.                     i = 0;
  19.                 }
  20.             }
  21.  
What should i do to remove that last record...

thanks and regards,
madhoriya22
Aug 10 '07 #3

10K+
P: 13,264
Hi Jos,
Tried ur way. I think it is working but the only problem is if the vector consists records that are all preexisted then while updating it leaves one record in the vector(according to condition it should be 0). Here is what I have done:-
Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i = 0; i < v.size(); i++) {
  3.                 pStatement.setString(1, ((DefectDetailVO)v.get(i)).getSeverity());
  4.                 pStatement.setString(2, ((DefectDetailVO)v.get(i)).getPriority());
  5.                 pStatement.setString(3, ((DefectDetailVO)v.get(i)).getAssignee());
  6.                 pStatement.setString(4, ((DefectDetailVO)v.get(i)).getStatus());
  7.                 pStatement.setString(5, ((DefectDetailVO)v.get(i)).getCurrentDefectStatus());
  8.                 pStatement.setString(6, ((DefectDetailVO)v.get(i)).getPhase());
  9.                 pStatement.setString(7, ((DefectDetailVO)v.get(i)).getTargetMilestone());
  10.                 pStatement.setString(8, ((DefectDetailVO)v.get(i)).getKeywords());
  11.                 pStatement.setString(9, ((DefectDetailVO)v.get(i)).getDefectId());
  12.  
  13.                 int status = pStatement.executeUpdate();
  14.  
  15.                 if(status >0) {
  16.                     System.out.println("Updated " +i);
  17.                     v.removeElementAt(i);
  18.                     i = 0;
  19.                 }
  20.             }
  21.  
What should i do to remove that last record...

thanks and regards,
madhoriya22
You may need to redesign your loop. Remember that i++ in your for loop, so your i = 0 does not mean the loop will start at 0 next time but at 1.
Also your loop is infinite if status is always greater than 0.
Aug 10 '07 #4

madhoriya22
100+
P: 252
You may need to redesign your loop. Remember that i++ in your for loop, so your i = 0 does not mean the loop will start at 0 next time but at 1.
Also your loop is infinite if status is always greater than 0.
hi,
thanks r035198x. got ur suggestion replace that for loop into while loop like this:-
Expand|Select|Wrap|Line Numbers
  1. while(i < v.size()) {
  2. ;
  3. ;
  4. ;//code
  5. int status = pStatement.executeUpdate();                
  6.       if(status >0) {
  7.            System.out.println("Updated " +i);
  8.            v.removeElementAt(i);
  9.            System.out.println("size after deletion:: "+v.size());                                      
  10.        }else {
  11.            i++;
  12.       }
  13. }
  14.  
Aug 10 '07 #5

Expert 10K+
P: 11,448
hi,
thanks r035198x. got ur suggestion replace that for loop into while loop like this:-
Expand|Select|Wrap|Line Numbers
  1. while(i < v.size()) {
  2. ;
  3. ;
  4. ;//code
  5. int status = pStatement.executeUpdate();                
  6.       if(status >0) {
  7.            System.out.println("Updated " +i);
  8.            v.removeElementAt(i);
  9.            System.out.println("size after deletion:: "+v.size());                                      
  10.        }else {
  11.            i++;
  12.       }
  13. }
  14.  
You could also just change loop counter i in your original version like this:

Expand|Select|Wrap|Line Numbers
  1.                 if(status >0) {
  2.                     System.out.println("Updated " +i);
  3.                     v.removeElementAt(i--); // <--- look here
  4.                 }
  5.             }
  6.  
kind regards,

Jos
Aug 10 '07 #6

10K+
P: 13,264
You could also just change loop counter i in your original version like this:

Expand|Select|Wrap|Line Numbers
  1.                 if(status >0) {
  2.                     System.out.println("Updated " +i);
  3.                     v.removeElementAt(i--); // <--- look here
  4.                 }
  5.             }
  6.  
kind regards,

Jos
The readability purists will sue you but that is definitely neater.
Aug 15 '07 #7

Expert 10K+
P: 11,448
The readability purists will sue you but that is definitely neater.
And all the normal people that don't like writing lots and lots of code will love
me for it ;-)

kind regards,

Jos
Aug 15 '07 #8

Post your reply

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