Examples of Java batch insert update data

  • 2020-04-01 04:04:47
  • OfStack

Java batch add data, multiple fields add multiple data at the same time, I don't know if you have encountered. Today we will take a specific example of batch add data in Java, for Oracle database.

Front page:


<span style="font-size:14px;"><body class="main_body" scroll="no"> 
<div class="employee_gun_dong"> 
  <form name="webform" method="post">   
      <div class="main_content_bg"> 
        <div class="main_content_title"> 
            <div id="baseinfo" onclick="activeTabPanel(1)" style="margin-right:2px;" class="tab1"> The basic information </div> 
            <div id="semsinfo" onclick="activeTabPanel(2)" style="margin-right:2px;" class="tab1" > TAB. 1</div> 
            <div id="changeinfo" onclick="activeTabPanel(3)"style="margin-right:2px;" class="tab1" > TAB. 2</div> 
            <div id="familyinfo" onclick="activeTabPanel(4)" style="margin-right:2px;" class="tab1" > TAB. 3</div> 
            <div id="changeinfo" onclick="activeTabPanel(5)"style="margin-right:2px;" class="tab1" > TAB. 4</div> 
            <div id="familyinfo" onclick="activeTabPanel(6)" style="margin-right:2px;" class="tab1" > TAB. 5</div> 
            <div id="familyinfo" onclick="activeTabPanel(7)" style="margin-right:2px;" class="tab2" > TAB. 6</div> 
        </div> 
      </div> 
       
      <div class="main_content_bg" id="bankCardDiv"> 
        <div class="main_middle_bg"> 
        <div class="main_content_title"> Information display   
          <input id="addBank" type="button" value=" Add a line " class="modul_button_operate" onclick="addNewRow();" /> 
        </div>                     
            <div class="main_table_bg"> 
            <table id="parttable" width='100%' border='0' cellspacing='1' cellpadding='0' class='content_table_list'>  
                <tr> 
                  <th class='content_table_number'> The serial number </th> 
                  <th class='content_table_thnowarp'> attribute 1</th> 
                  <th class='content_table_thnowarp'> attribute 2</th> 
                  <th class='content_table_thnowarp'> attribute 3</th> 
                  <th class='content_table_thnowarp'> attribute 4</th> 
                  <th class='content_table_thnowarp'> attribute 5</th> 
                  <th class='content_table_thnowarp'> attribute 6</th> 
                  <th class='content_table_thnowarp'> attribute 7</th> 
                  <th class='content_table_thnowarp'> attribute 8</th> 
                  <th class='content_table_thnowarp'> attribute 9</th> 
                  <th class='content_table_thnowarp'> operation </th> 
                </tr> 
                <c:if test="${fn:length(personFamilyInfoList) > 0}"> 
                <c:forEach items="${personFamilyInfoList}" var="personFamilyInfoList" varStatus="st">  
                               
                <tr> 
                  <!--  The serial number  --> 
                  <td class="content_table_number">${st.count} 
                    <input type="hidden" name="personFamilyInfoList[${st.count-1}].SGuid" value="${personFamilyInfoList.SGuid}"/> 
                    <input type="hidden" name="personFamilyInfoList[${st.count-1}].SPersonGuid" value="${personFamilyInfoList.SPersonGuid}"/>                    
                    <input type="hidden" name="personFamilyInfoList[${st.count-1}].SUnitGuid" value="${personFamilyInfoList.SUnitGuid }"> 
                    <input type="hidden" name="personFamilyInfoList[${st.count-1}].isEnable" value="${personFamilyInfoList.isEnable }"> 
                  </td>  
                  <!--  attribute 1--> 
                  <td class="content_table_td_centernowrap"> 
                    <input id="sFamilyName${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].SFamilyName" class="content_content_input" maxlength="20" value="${personFamilyInfoList.SFamilyName}"> 
                  </td> 
                  <!--  attribute 2--> 
                  <td class="content_table_td_centernowrap"> 
                    <input id="sFamilyIdcardNo${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].SFamilyIdcardNo" class="content_content_input" maxlength="20" value="${personFamilyInfoList.SFamilyIdcardNo}"> 
                  </td> 
                  <!--  attribute 3-->                   
                  <td class="content_table_td_centernowrap"> 
                    <select id="iFamilySex${st.count-1}" name="personFamilyInfoList[${st.count-1}].IFamilySex"> 
                      <zw:basedictlist itemCode="<%=Constants.I_PERSON_SEX %>" selectValue="${personFamilyInfoList.IFamilySex}"></zw:basedictlist> 
                    </select><font color="red">*</font> 
                  </td> 
                  <!--  attribute 4--> 
                  <td class="content_table_td_centernowrap"> 
                    <select id="sFamilyRelation${st.count-1}" name="personFamilyInfoList[${st.count-1}].SFamilyRelation"> 
                      <zw:basedictlist itemCode="<%=Constants.S_FAMILY_RELATION %>" selectValue="${personFamilyInfoList.SFamilyRelation}"></zw:basedictlist> 
                    </select><font color="red">*</font> 
                  </td> 
                  <!--  attribute 5-->     
                  <td class="content_table_td_centernowrap"> 
                    <input id="sFamilyInsurancePlace${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].SFamilyInsurancePlace" class="content_content_input" maxlength="20" value="${personFamilyInfoList.SFamilyInsurancePlace}">   
                  </td> 
                  <!--  attribute 6--> 
                  <td class='content_table_td_centernowrap'> 
                    <input id="sFamilyResidencePalce${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].SFamilyResidencePalce" class="content_content_input" maxlength="20" value="${personFamilyInfoList.SFamilyResidencePalce}"> 
                  </td> 
                  <!--  attribute 7--> 
                  <td class="content_table_td_centernowrap"> 
                    <input id="sFamilyPhone${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].SFamilyPhone" class="content_content_input" maxlength="20" value="${personFamilyInfoList.SFamilyPhone}"> 
                  </td> 
                  <!--  attribute 8--> 
                  <td class="content_table_td_centernowrap"> 
                    <input id="dDentifySucessDate${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].DDentifySucessDate" value="${personFamilyInfoList.DDentifySucessDate}" onfocus="WdatePicker()" notnull="true" vdisp=" First enrollment period " class="content_content_input" size="18" maxlength="18" style="width:70px;"/> 
                    <font color="red">*</font> 
                  </td> 
                  <!--  attribute 9--> 
                  <td class="content_table_td_centernowrap"> 
                    <input id="dDentifyLostDate${st.count-1}" type="text" name="personFamilyInfoList[${st.count - 1}].DDentifyLostDate" value="${personFamilyInfoList.DDentifyLostDate}" onfocus="WdatePicker()" notnull="true" vdisp=" First enrollment period " class="content_content_input" size="18" maxlength="18" style="width:70px;"/> 
                  </td> 
                  <td class='content_table_td_centernowrap'> cancel </td>   
                </tr> 
                 
                </c:forEach> 
                </c:if>                                       
              </table> 
          </div>           
        </div> 
      </div> 
      <div class="main_content_bg"> 
      <div class="main_content_title"> 
        <table style="width:100%"> 
          <tr> 
            <td width="100%" align="center"> 
              <input id="btnNextstep" type="button" value=" The previous step " class="modul_button_operate" onclick="" /> 
              <input id="btnAdd" type="button" value=" save " class="modul_button_operate" onclick="saveOrUpdate()" /> 
              <input id="" type="button" value=" complete " class="modul_button_operate" onclick="" /> 
              <input id="btnReturn" type="button" value=" return " class="modul_button_operate" onclick="" /> 
            </td>            
          </tr> 
        </table> 
      </div> 
     </div> 
  </form>   
</div> 
</body></span> 

Javascript function:


<span style="font-family:KaiTi_GB2312;font-size:14px;"><script type="text/javascript"> 
  $(function(){ 
    loadCheck(); 
  }); 
 
  function loadCheck(){ 
    var trs = $('#parttable tr').length; 
    if(trs == 1){ 
    addNewRow(); 
    } 
  } 
 
  //The initial variable
  var num = 0;//Page count variable
  var row = 1;//Line increment count variable
  var index = 2;//List counts variables
  function addNewRow(){ 
    var trNum = $('#parttable tr').length; 
    if(trNum>1){ 
      row = trNum; 
      num = trNum - 1; 
    } 
    var htmlText ='<tr>' 
      +'<td class="content_table_number">'+row+'</td>' 
 
      +'<td class="content_table_td_centernowrap">' 
      +'<input id="sFamilyName'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyName" class="content_content_input" maxlength="20"></td>'  
 
      +'<td class="content_table_td_centernowrap">' 
      +'<input id="sFamilyIdcardNo'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyIdcardNo" class="content_content_input" maxlength="20"></td>' 
               
      +'<td class="content_table_td_centernowrap">' 
      +'<select id="iFamilySex'+num+'" name="personFamilyInfoList['+num+'].IFamilySex">' 
      +'<zw:basedictlist itemCode="2000" selectValue="personFamilyInfoList['+num+'].IFamilySex"></zw:basedictlist>' 
      +'</select><font color="red">*</font></td>' 
 
      +'<td class="content_table_td_centernowrap">' 
      +'<select id="sFamilyRelation'+num+'" name="personFamilyInfoList['+num+'].SFamilyRelation">' 
      +'<zw:basedictlist itemCode="135" selectValue="personFamilyInfoList['+num+'].SFamilyRelation"></zw:basedictlist>' 
      +'</select><font color="red">*</font></td>' 
   
      +'<td class="content_table_td_centernowrap">' 
      +'<input id="sFamilyInsurancePlace'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyInsurancePlace" class="content_content_input" maxlength="20"></td>' 
       
      +'<td class="content_table_td_centernowrap">' 
      +'<input id="sFamilyResidencePalce'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyResidencePalce" class="content_content_input" maxlength="20"></td>' 
 
      +'<td class="content_table_td_centernowrap">' 
      +'<input id="sFamilyPhone'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyPhone" class="content_content_input" maxlength="20"></td>' 
       
      +'<td class="content_table_td_centernowrap">' 
      +'<input id="dDentifySucessDate'+num+'" type="text" name="personFamilyInfoList['+num+'].DDentifySucessDate" onfocus="WdatePicker()" notnull="true" vdisp=" First enrollment period " class="content_content_input" size="18" maxlength="18" style="width:70px;"/><font color="red">*</font></td>' 
 
      +'<td class="content_table_td_centernowrap">' 
      +'<input id="dDentifyLostDate'+num+'" type="text" name="personFamilyInfoList['+num+'].DDentifyLostDate" onfocus="WdatePicker()" notnull="true" vdisp=" First enrollment period " class="content_content_input" size="18" maxlength="18" style="width:70px;"/></td>'     
       
      +'<td class="content_table_td_centernowrap">'      
      +'<a onclick="delInsuranceInfo(this);"> cancel </a>' 
      +'</td>' 
      +'</tr>'; 
    $("#parttable").append(htmlText); 
    num = num + 1; 
    row += 1; 
  } 
 
  //Delete dynamic list
  function delInsuranceInfo(t){ 
    if(row>0){row=row-1} else{return false;} 
    $(t).parent().parent().remove(); 
  } 
 
  //Save or modify
  function saveOrUpdate(){ 
    $.ajax({ 
      url: "personFamilyInfo_addOrUpdatePersonFamilyInfo_include_json.action", 
      type: "POST", 
      data: jQuery(document.forms[0]).serializeArray(), 
      success: function(resObj) { 
        //Determine the return value
        if (resObj.trim() == 'true') { 
          Ext.MessageBox.alert(" prompt "," Save success ",function(){ 
             
          });                                
        }else if(resObj.trim() == 'update'){ 
          Ext.MessageBox.alert(" prompt "," The update is successful ",function(){ 
 
          }); 
        } else { 
          Ext.MessageBox.alert(" prompt "," Save failed "); 
        } 
      } 
    }); 
  }      </span> 
<span style="font-family:KaiTi_GB2312;font-size:14px;"></script></span> 

Background adding method:


<span style="font-family:KaiTi_GB2312;font-size:14px;">/** 
   * <p>Description:  Add multiple messages in batches </p> 
   * @param personFamilyInfoList  An entity list  
   * @return  Boolean value, true To add success, otherwise to add failure  
   * @throws Exception 
   * @author    : gaoying 
   * @update    : 
   * @date     : 2015-7-20 
   */ 
  public boolean addPersonFamilyInfo(List<PersonFamilyInfo> personFamilyInfoList) throws Exception{ 
    boolean bool = false; 
    if(personFamilyInfoList.size()>0 && personFamilyInfoList != null){ 
      for(int i=0; i<personFamilyInfoList.size(); i++){ 
        String personFamilyID = UUIDHexGenerator.getUUID(); 
        String sunitGuid = "11111111"; 
        String spersonGuid = "0000000"; 
        if(personFamilyInfoList.get(i) != null){ 
          personFamilyInfoList.get(i).setSGuid(personFamilyID);   
          personFamilyInfoList.get(i).setSUnitGuid(sunitGuid); 
          personFamilyInfoList.get(i).setSPersonGuid(spersonGuid);   
          personFamilyInfoList.get(i).setIsEnable(0); 
          personFamilyInfoList.get(i).setDOperateDate(new Date()); 
          //Set the current time of the system as the effective time and the failure time as null
          personFamilyInfoList.get(i).setDDentifySucessDate(new Date()); 
          personFamilyInfoList.get(i).setDDentifyLostDate(null);      
          try { 
            String sGuidString = personFamilyInfoService.saveNeedPk(personFamilyInfoList.get(i)).toString(); 
            if(!"".equals(sGuidString)&&sGuidString.length()>0){ 
              bool = true; 
            }else{ 
              System.out.println(" Information sheet on dependent relatives   The first "+i+" Message storage failed! "); 
              bool = false; 
              return bool; 
            } 
 
          } catch (Exception e) { 
            e.printStackTrace(); 
          } 
        } 
      } 
       
    } 
    return bool;     
  }</span> 

Background update method:


<span style="font-family:KaiTi_GB2312;font-size:14px;">/** 
   * <p>Description:  Batch update information </p> 
   * @param personFamilyInfolist  An entity list  
   * @param personId  personnel id 
   * @return  Boolean value, true Represents the update succeeds, otherwise the update fails  
   * @throws Exception 
   * @author    : gaoying 
   * @update    : 
   * @date     : 2015-7-20 
   */ 
  public void updatePersonFamilyInfo(List<PersonFamilyInfo> personFamilyInfolist, String personId) throws Exception{ 
    //Query how many pieces of data there are on the page based on the person id
    List<PersonFamilyInfo> oldPersonFamilyInfolist = personFamilyInfoService.getPersonFamilyInfoByPersonId(personId); 
    int num = oldPersonFamilyInfolist.size(); 
    if(personFamilyInfolist != null&&personFamilyInfolist.size() > 0){ 
      //Check if the information on the page has changed, and then update it to the database
      for(int i = 0; i<num; i++){ 
        try{ 
          //Set a Boolean value to false if the field is changed or true if it is not changed
          boolean bool = true; 
          //Determine which fields have changed
          if(!oldPersonFamilyInfolist.get(i).getSFamilyName().equals(personFamilyInfolist.get(i).getSFamilyName())){ 
            bool = false; 
          } 
          if(!oldPersonFamilyInfolist.get(i).getSFamilyIdcardNo().equals(personFamilyInfolist.get(i).getSFamilyIdcardNo())){ 
            bool = false; 
          } 
          if(!oldPersonFamilyInfolist.get(i).getIFamilySex().equals(personFamilyInfolist.get(i).getIFamilySex())){ 
            bool = false; 
          } 
          if(!oldPersonFamilyInfolist.get(i).getSFamilyRelation().equals(personFamilyInfolist.get(i).getSFamilyRelation())){ 
            bool = false; 
          } 
          if(!oldPersonFamilyInfolist.get(i).getSFamilyInsurancePlace().equals(personFamilyInfolist.get(i).getSFamilyInsurancePlace())){ 
            bool = false; 
          } 
          if(!oldPersonFamilyInfolist.get(i).getSFamilyResidencePalce().equals(personFamilyInfolist.get(i).getSFamilyResidencePalce())){ 
            bool = false; 
          } 
          if(!oldPersonFamilyInfolist.get(i).getSFamilyPhone().equals(personFamilyInfolist.get(i).getSFamilyPhone())){ 
            bool = false; 
          } 
          System.out.println(" Time before change: "+oldPersonFamilyInfolist.get(i).getDDentifySucessDate().getTime()); 
          System.out.println(" Time after modification: "+personFamilyInfolist.get(i).getDDentifySucessDate().getTime()); 
          if(oldPersonFamilyInfolist.get(i).getDDentifySucessDate().getTime() != personFamilyInfolist.get(i).getDDentifySucessDate().getTime()){ 
            bool = false; 
          } 
           
          if(bool == false){//Prove that the field has changed
            //Before updating the data, copy the original unchanged data, add it to the database, set the current time of the system as the expiration time of the data
            PersonFamilyInfo personFamilyInfo = new PersonFamilyInfo(); 
             
            BeanUtils.copyProperties(oldPersonFamilyInfolist.get(i), personFamilyInfo); 
            System.out.println(" This data primary key: " + personFamilyInfo.getSGuid()); 
            personFamilyInfo.setSGuid(UUIDHexGenerator.getUUID()); 
            System.out.println(" Set primary key: " + personFamilyInfo.getSGuid()); 
            personFamilyInfo.setDOperateDate(new Date()); 
            //Set the current save database to 1: invalid
            personFamilyInfo.setIsEnable(1); 
            personFamilyInfo.setDDentifyLostDate(new Date());             
             
            personFamilyInfoService.save(personFamilyInfo); 
             
            //Update this data, set the current time of the system as the effective time and operation time of the system, set it as 0: effective, and the failure time as null
            personFamilyInfolist.get(i).setIsEnable(0);   
            personFamilyInfolist.get(i).setDDentifySucessDate(new Date()); 
            personFamilyInfolist.get(i).setDOperateDate(new Date()); 
            personFamilyInfolist.get(i).setDDentifyLostDate(null); 
            personFamilyInfoService.merge(personFamilyInfolist.get(i)); 
          }                             
        }catch (Exception e) { 
          e.printStackTrace(); 
        }  
               
      } 
    }</span>  

In this way, the example of Java multi-field and multi-data batch addition is completed. It is mainly necessary to pay attention to the multiline js function in the foreground page superposition and the adding and updating methods in the background. It is received by list and added by loop traversal. In addition, I would like to mention the following code:

<span style="font-family:KaiTi_GB2312;font-size:14px;">$(function(){  
    loadCheck(); 
});</span>  
   

It stands for:

<span style="font-family:KaiTi_GB2312;font-size:14px;"> $(document).ready(function(){  
        loadCheck(); 
});</span> 

This code is the same as the following code:

<span style="font-family:KaiTi_GB2312;font-size:14px;">window.onload=function(){  
       loadCheck(); 
}</span>
     

So instead of calling it in the body onload event, you just write it in the js section. Although the following code is interchangeable with the above two, there are differences between them. The first is that the execution time is different, $(document).ready is executed after the page frame has downloaded, while window.onload cannot be executed until the page is fully loaded (including picture download). Obviously the former is more efficient than the latter. $(document). Ready can be written more than once, and the result of each execution is different. While window.onload, although it can execute more than one, can only output the last execution result and cannot complete the output of multiple results.

The above is the entire content of this article, I hope to help you with your study.


Related articles: