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:- -
-
rs = pStatement.executeQuery();
-
-
while(rs.next()) {
-
for(int i = 0; i < v.size(); i++) {
-
if(((DefectDetailVO)v.get(i)).getDefectId().equals(rs.getString("DEFECT_ID"))) {
-
System.out.println("Is Record Updated::: " +updateExistingDefectDetails((DefectDetailVO)v.get(i)));
-
v.removeElementAt(i);//Here v is a vector and consists records coming from CSV file
-
break;
-
}
-
}
-
}
-
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.
7 3328
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
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:- -
-
for(int i = 0; i < v.size(); i++) {
-
pStatement.setString(1, ((DefectDetailVO)v.get(i)).getSeverity());
-
pStatement.setString(2, ((DefectDetailVO)v.get(i)).getPriority());
-
pStatement.setString(3, ((DefectDetailVO)v.get(i)).getAssignee());
-
pStatement.setString(4, ((DefectDetailVO)v.get(i)).getStatus());
-
pStatement.setString(5, ((DefectDetailVO)v.get(i)).getCurrentDefectStatus());
-
pStatement.setString(6, ((DefectDetailVO)v.get(i)).getPhase());
-
pStatement.setString(7, ((DefectDetailVO)v.get(i)).getTargetMilestone());
-
pStatement.setString(8, ((DefectDetailVO)v.get(i)).getKeywords());
-
pStatement.setString(9, ((DefectDetailVO)v.get(i)).getDefectId());
-
-
int status = pStatement.executeUpdate();
-
-
if(status >0) {
-
System.out.println("Updated " +i);
-
v.removeElementAt(i);
-
i = 0;
-
}
-
}
-
What should i do to remove that last record...
thanks and regards,
madhoriya22
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:- -
-
for(int i = 0; i < v.size(); i++) {
-
pStatement.setString(1, ((DefectDetailVO)v.get(i)).getSeverity());
-
pStatement.setString(2, ((DefectDetailVO)v.get(i)).getPriority());
-
pStatement.setString(3, ((DefectDetailVO)v.get(i)).getAssignee());
-
pStatement.setString(4, ((DefectDetailVO)v.get(i)).getStatus());
-
pStatement.setString(5, ((DefectDetailVO)v.get(i)).getCurrentDefectStatus());
-
pStatement.setString(6, ((DefectDetailVO)v.get(i)).getPhase());
-
pStatement.setString(7, ((DefectDetailVO)v.get(i)).getTargetMilestone());
-
pStatement.setString(8, ((DefectDetailVO)v.get(i)).getKeywords());
-
pStatement.setString(9, ((DefectDetailVO)v.get(i)).getDefectId());
-
-
int status = pStatement.executeUpdate();
-
-
if(status >0) {
-
System.out.println("Updated " +i);
-
v.removeElementAt(i);
-
i = 0;
-
}
-
}
-
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.
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:- -
while(i < v.size()) {
-
;
-
;
-
;//code
-
int status = pStatement.executeUpdate();
-
if(status >0) {
-
System.out.println("Updated " +i);
-
v.removeElementAt(i);
-
System.out.println("size after deletion:: "+v.size());
-
}else {
-
i++;
-
}
-
}
-
hi,
thanks r035198x. got ur suggestion replace that for loop into while loop like this:- -
while(i < v.size()) {
-
;
-
;
-
;//code
-
int status = pStatement.executeUpdate();
-
if(status >0) {
-
System.out.println("Updated " +i);
-
v.removeElementAt(i);
-
System.out.println("size after deletion:: "+v.size());
-
}else {
-
i++;
-
}
-
}
-
You could also just change loop counter i in your original version like this: -
if(status >0) {
-
System.out.println("Updated " +i);
-
v.removeElementAt(i--); // <--- look here
-
}
-
}
-
kind regards,
Jos
You could also just change loop counter i in your original version like this: -
if(status >0) {
-
System.out.println("Updated " +i);
-
v.removeElementAt(i--); // <--- look here
-
}
-
}
-
kind regards,
Jos
The readability purists will sue you but that is definitely neater.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
by: noname |
last post by:
?????
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |