473,405 Members | 2,279 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,405 software developers and data experts.

How to overwrite existing records

madhoriya22
252 100+
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
7 3328
JosAH
11,448 Expert 8TB
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
252 100+
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
r035198x
13,262 8TB
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
252 100+
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
JosAH
11,448 Expert 8TB
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
r035198x
13,262 8TB
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

5
by: Gord | last post by:
Hello, If you set the flag for an overwrite prompt using the 'Save' common dialog, how do you read the response when the user clicks the Yes or No in the 'overwrite' message box? Everything...
1
by: Bernhard Hidding | last post by:
Hello, my program writes an array into a file using the following code: ofstream arrayfile; arrayfile.open("array_file.dat"); .... arraydatei.close(); This works as long as the file does...
11
by: Florian Loitsch | last post by:
I'm currently writing a JS->Scheme compiler (which, using Bigloo, automatically yields a JS->C, JS->JVM, JS->.NET compiler), and have a question concerning the function-parameters: According to...
2
by: kiwichico | last post by:
Hi everyone, I am trying to write some VBA code to overwrite duplicates in an existing table with records that are imported. However I don't want to overwrite the fields in the exisiting records...
2
by: Colleyville Alan | last post by:
I have an app in which a ListBox is using a table called "Unique_Plans" as its RowSource. On another form, I create some changes to one table and then use a make-table query on the changed table...
6
by: deko | last post by:
Is there a way to set a custom property on Access tables and/or queries to prevent them from being overwritten by import wizards? Any Access database can be easily destroyed if a user mistakenly...
2
by: B-Dog | last post by:
Is there a way to make vb.net to overwrite the file when moving? Here is what I'm trying to do: If System.IO.File.Exists(dest) Then 'handle overwrite here If MessageBox.Show("Do you want...
5
by: Ben Sizer | last post by:
I need to copy directories from one place to another, but it needs to overwrite individual files and directories rather than just exiting if a destination file already exists. Previous suggestions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.