程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 實例講授Java批量拔出、更新數據

實例講授Java批量拔出、更新數據

編輯:關於JAVA

實例講授Java批量拔出、更新數據。本站提示廣大學習愛好者:(實例講授Java批量拔出、更新數據)文章只能為提供參考,不一定能成為您想要的結果。以下是實例講授Java批量拔出、更新數據正文


Java的批量添加數據,多個字段同時添加多條數據,我不曉得你碰到過沒有。明天我們就以一個詳細的實例來講一下Java的批量添加數據,面向的是Oracle數據庫。

前台頁面:

<span ><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)"  class="tab1">根本信息</div> 
            <div id="semsinfo" onclick="activeTabPanel(2)"  class="tab1" >頁簽1</div> 
            <div id="changeinfo" onclick="activeTabPanel(3)" class="tab1" >頁簽2</div> 
            <div id="familyinfo" onclick="activeTabPanel(4)"  class="tab1" >頁簽3</div> 
            <div id="changeinfo" onclick="activeTabPanel(5)" class="tab1" >頁簽4</div> 
            <div id="familyinfo" onclick="activeTabPanel(6)"  class="tab1" >頁簽5</div> 
            <div id="familyinfo" onclick="activeTabPanel(7)"  class="tab2" >頁簽6</div> 
        </div> 
      </div> 
       
      <div class="main_content_bg" id="bankCardDiv"> 
        <div class="main_middle_bg"> 
        <div class="main_content_title">信息展現  
          <input id="addBank" type="button" value="新增一行" 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'>序號</th> 
                  <th class='content_table_thnowarp'>屬性1</th> 
                  <th class='content_table_thnowarp'>屬性2</th> 
                  <th class='content_table_thnowarp'>屬性3</th> 
                  <th class='content_table_thnowarp'>屬性4</th> 
                  <th class='content_table_thnowarp'>屬性5</th> 
                  <th class='content_table_thnowarp'>屬性6</th> 
                  <th class='content_table_thnowarp'>屬性7</th> 
                  <th class='content_table_thnowarp'>屬性8</th> 
                  <th class='content_table_thnowarp'>屬性9</th> 
                  <th class='content_table_thnowarp'>操作</th> 
                </tr> 
                <c:if test="${fn:length(personFamilyInfoList) > 0}"> 
                <c:forEach items="${personFamilyInfoList}" var="personFamilyInfoList" varStatus="st">  
                               
                <tr> 
                  <!-- 序號 --> 
                  <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>  
                  <!-- 屬性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> 
                  <!-- 屬性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> 
                  <!-- 屬性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> 
                  <!-- 屬性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> 
                  <!-- 屬性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> 
                  <!-- 屬性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> 
                  <!-- 屬性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> 
                  <!-- 屬性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="初次參保時光" class="content_content_input" size="18" maxlength="18" /> 
                    <font color="red">*</font> 
                  </td> 
                  <!-- 屬性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="初次參保時光" class="content_content_input" size="18" maxlength="18" /> 
                  </td> 
                  <td class='content_table_td_centernowrap'>撤消</td>   
                </tr> 
                 
                </c:forEach> 
                </c:if>                                       
              </table> 
          </div>           
        </div> 
      </div> 
      <div class="main_content_bg"> 
      <div class="main_content_title"> 
        <table > 
          <tr> 
            <td width="100%" align="center"> 
              <input id="btnNextstep" type="button" value="上一步" class="modul_button_operate" onclick="" /> 
              <input id="btnAdd" type="button" value="保留" class="modul_button_operate" onclick="saveOrUpdate()" /> 
              <input id="" type="button" value="完成" class="modul_button_operate" onclick="" /> 
              <input id="btnReturn" type="button" value="前往" class="modul_button_operate" onclick="" /> 
            </td>            
          </tr> 
        </table> 
      </div> 
     </div> 
  </form>   
</div> 
</body></span> 

javascript函數:

<span ><script type="text/javascript"> 
  $(function(){ 
    loadCheck(); 
  }); 
 
  function loadCheck(){ 
    var trs = $('#parttable tr').length; 
    if(trs == 1){ 
    addNewRow(); 
    } 
  } 
 
  //初始變量 
  var num = 0;//頁面計數變量 
  var row = 1;//行增長計數變量 
  var index = 2;//List列表計數變量 
  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="初次參保時光" class="content_content_input" size="18" maxlength="18" /><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="初次參保時光" class="content_content_input" size="18" maxlength="18" /></td>'     
       
      +'<td class="content_table_td_centernowrap">'      
      +'<a onclick="delInsuranceInfo(this);">撤消</a>' 
      +'</td>' 
      +'</tr>'; 
    $("#parttable").append(htmlText); 
    num = num + 1; 
    row += 1; 
  } 
 
  //刪除靜態列表 
  function delInsuranceInfo(t){ 
    if(row>0){row=row-1} else{return false;} 
    $(t).parent().parent().remove(); 
  } 
 
  //保留或修正 
  function saveOrUpdate(){ 
    $.ajax({ 
      url: "personFamilyInfo_addOrUpdatePersonFamilyInfo_include_json.action", 
      type: "POST", 
      data: jQuery(document.forms[0]).serializeArray(), 
      success: function(resObj) { 
        //斷定前往值 
        if (resObj.trim() == 'true') { 
          Ext.MessageBox.alert("提醒","保留勝利",function(){ 
             
          });                                
        }else if(resObj.trim() == 'update'){ 
          Ext.MessageBox.alert("提醒","更新勝利",function(){ 
 
          }); 
        } else { 
          Ext.MessageBox.alert("提醒","保留掉敗"); 
        } 
      } 
    }); 
  }      </span> 
<span ></script></span> 

後台添加辦法:

<span >/** 
   * <p>Description: 批量添加多條信息</p> 
   * @param personFamilyInfoList 實體列表 
   * @return 布爾值,true為添加勝利,不然為添加掉敗 
   * @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()); 
          //設置體系確當前時光為失效時光,掉效時光置為空 
          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("贍養親屬信息表 第"+i+"條信息存儲掉敗!"); 
              bool = false; 
              return bool; 
            } 
 
          } catch (Exception e) { 
            e.printStackTrace(); 
          } 
        } 
      } 
       
    } 
    return bool;     
  }</span> 

後台更新辦法:

<span >/** 
   * <p>Description: 批量更新信息</p> 
   * @param personFamilyInfolist 實體列表 
   * @param personId 人員id 
   * @return 布爾值,true代表更新勝利,不然更新掉敗 
   * @throws Exception 
   * @author    : gaoying 
   * @update    : 
   * @date     : 2015-7-20 
   */ 
  public void updatePersonFamilyInfo(List<PersonFamilyInfo> personFamilyInfolist, String personId) throws Exception{ 
    //依據人員id查詢頁面有若干條數據 
    List<PersonFamilyInfo> oldPersonFamilyInfolist = personFamilyInfoService.getPersonFamilyInfoByPersonId(personId); 
    int num = oldPersonFamilyInfolist.size(); 
    if(personFamilyInfolist != null&&personFamilyInfolist.size() > 0){ 
      //檢討頁面的信息能否有修正,然後更新到數據庫 
      for(int i = 0; i<num; i++){ 
        try{ 
          //設置一個布爾值,假如更改字段設置為false,假如不更改則設置為true 
          boolean bool = true; 
          //斷定哪些字段更悛改 
          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("未更改前時光:"+oldPersonFamilyInfolist.get(i).getDDentifySucessDate().getTime()); 
          System.out.println("更改後的時光:"+personFamilyInfolist.get(i).getDDentifySucessDate().getTime()); 
          if(oldPersonFamilyInfolist.get(i).getDDentifySucessDate().getTime() != personFamilyInfolist.get(i).getDDentifySucessDate().getTime()){ 
            bool = false; 
          } 
           
          if(bool == false){//證實字段更悛改 
            //更新數據前,要把本來沒修改的數據復制一條,添加進數據庫,把體系確當前時光設為該條數據的掉效時光         
            PersonFamilyInfo personFamilyInfo = new PersonFamilyInfo(); 
             
            BeanUtils.copyProperties(oldPersonFamilyInfolist.get(i), personFamilyInfo); 
            System.out.println("該條數據主鍵:" + personFamilyInfo.getSGuid()); 
            personFamilyInfo.setSGuid(UUIDHexGenerator.getUUID()); 
            System.out.println("設置主鍵:" + personFamilyInfo.getSGuid()); 
            personFamilyInfo.setDOperateDate(new Date()); 
            //將以後保留數據庫的能否有用置為1:有效 
            personFamilyInfo.setIsEnable(1); 
            personFamilyInfo.setDDentifyLostDate(new Date());             
             
            personFamilyInfoService.save(personFamilyInfo); 
             
            //更新該條數據,把體系確當前時光設為體系的失效時光和操作時光,能否有用設為0:有用,掉效時光為空 
            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>  

如許,java多字段、多條數據批量添加的例子就完成了,重要要留意前台頁面疊加湧現多行的js函數和後台的添加和更新辦法,用list吸收,輪回遍歷停止添加。除此以外這裡我想說一下上面這段代碼:

<span >$(function(){ 
    loadCheck(); 
});</span>    

它就是以下代碼的縮寫:

<span > $(document).ready(function(){ 
        loadCheck(); 
});</span> 

其實這個代碼和上面的代碼是一個意思:

<span >window.onload=function(){ 
       loadCheck(); 
}</span>   

如許就不消在body的onload事宜外面挪用,只須要在js法式段外面編寫便可以了。固然上面的代碼可以和下面兩個交換,但他們之間又有分歧。起首是履行時光分歧,$(document).ready在頁面框架下載終了後就履行,而window.onload必需在頁面全體加載終了(包括圖片下載)後能力履行。很顯著前者的履行效力高於後者。再就是履行數目的分歧,$(document).ready可以反復寫多個,並且每次履行成果分歧;而window.onload雖然可以履行多個,但僅輸入最初一個履行成果,沒法完成多個成果的輸入。

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved