How to overwrite existing records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    How to overwrite existing records

    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:-
    Code:
     
    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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • madhoriya22
      Contributor
      • Jul 2007
      • 251

      #3
      Originally posted by JosAH
      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(accordin g to condition it should be 0). Here is what I have done:-
      Code:
        
      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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by 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(accordin g to condition it should be 0). Here is what I have done:-
        Code:
          
        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.

        Comment

        • madhoriya22
          Contributor
          • Jul 2007
          • 251

          #5
          Originally posted by r035198x
          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:-
          Code:
          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++;
          	  }
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by madhoriya22
            hi,
            thanks r035198x. got ur suggestion replace that for loop into while loop like this:-
            Code:
            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:

            [code=java]
            if(status >0) {
            System.out.prin tln("Updated " +i);
            v.removeElement At(i--); // <--- look here
            }
            }
            [/code]

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              You could also just change loop counter i in your original version like this:

              [code=java]
              if(status >0) {
              System.out.prin tln("Updated " +i);
              v.removeElement At(i--); // <--- look here
              }
              }
              [/code]

              kind regards,

              Jos
              The readability purists will sue you but that is definitely neater.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                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

                Comment

                Working...