Java的批量添加數據,多個字段同時添加多條數據,我不知道你遇到過沒有。今天我們就以一個具體的實例來說一下Java的批量添加數據,面向的是Oracle數據庫。
前台頁面:
<span style="font-size:14px;"><body scroll="no"> <div> <form name="webform" method="post"> <div> <div> <div id="baseinfo" onclick="activeTabPanel(1)" style="margin-right:2px;">基本信息</div> <div id="semsinfo" onclick="activeTabPanel(2)" style="margin-right:2px;" >頁簽1</div> <div id="changeinfo" onclick="activeTabPanel(3)"style="margin-right:2px;" >頁簽2</div> <div id="familyinfo" onclick= "activeTabPanel(4)" style="margin-right:2px;" >頁簽3</div> <div id="changeinfo" onclick="activeTabPanel(5)"style="margin-right:2px;" >頁簽4</div> <div id="familyinfo" onclick="activeTabPanel(6)" style="margin-right:2px;" >頁簽5</div> <div id="familyinfo" onclick=" activeTabPanel(7)" style="margin-right:2px;" >頁簽6</div> </div> </div> <div id="bankCardDiv"> <div> <div>信息展示<input id ="addBank" type="button" value="新增一行" onclick="addNewRow();" /> </div> <div> <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>${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> <input id ="sFamilyName${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].SFamilyName" maxlength="20" value="${personFamilyInfoList.SFamilyName}"> </td> <!-- 屬性2--> <td> <input id="sFamilyIdcardNo${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}] .SFamilyIdcardNo" maxlength="20" value="${personFamilyInfoList.SFamilyIdcardNo}"> </td> <!-- 屬性3--> <td> <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> <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> <input id="sFamilyInsurancePlace${st.count-1}" type= "text" name="personFamilyInfoList[${st.count-1}].SFamilyInsurancePlace" 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" maxlength="20" value ="${personFamilyInfoList.SFamilyResidencePalce}"> </td> <!-- 屬性7--> <td> <input id="sFamilyPhone${st.count-1}" type="text" name="personFamilyInfoList [${st.count-1}].SFamilyPhone" maxlength="20" value="${personFamilyInfoList.SFamilyPhone}"> </td> <!-- 屬性8--> <td> <input id=" dDentifySucessDate${st.count-1}" type="text" name="personFamilyInfoList[${st.count-1}].DDentifySucessDate" value="${personFamilyInfoList.DDentifySucessDate}" onfocus="WdatePicker()" notnull ="true" vdisp="首次參保時間" size="18" maxlength="18"/> <font color="red">*</font> </td> <!-- 屬性9--> <td> <input id="dDentifyLostDate${st.count-1}" type="text" name="personFamilyInfoList[${st.count - 1}].DDentifyLostDate" value="${personFamilyInfoList.DDentifyLostDate}" onfocus="WdatePicker()" notnull="true" vdisp="首次參保時間" size="18" maxlength="18"/> </td> <td class='content_table_td_centernowrap'>取消</td> < /tr> </c:forEach> </c:if> </table> </div> </div> </div> <div> <div> <table> <tr> <td align="center" > <input id="btnNextstep" type="button" value="上一步" onclick="" /> <input id="btnAdd" type="button" value="保存" onclick="saveOrUpdate()" / > <input id="" type="button" value="完成" onclick="" /> <input id="btnReturn" type="button" value="返回" onclick="" /> </td> </tr> </table> </div> </div> </form> </div> </body></span>
javascript函數:
<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(); } } //初始變量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>'+row+'</td>' +'<td>' +'<input id="sFamilyName'+num+'" type="text" name="personFamilyInfoList[' +num+'].SFamilyName" maxlength="20"></td>' +'<td>' +'<input id="sFamilyIdcardNo'+num+'" type="text" name="personFamilyInfoList['+num+ '].SFamilyIdcardNo" maxlength="20"></td>' +'<td>' +'<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>' +'<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 >' +'<input id="sFamilyInsurancePlace'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyInsurancePlace" maxlength="20"></td>' +'<td>' +'<input id="sFamilyResidencePalce'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyResidencePalce" maxlength="20"></td>' +'<td>' +' <input id="sFamilyPhone'+num+'" type="text" name="personFamilyInfoList['+num+'].SFamilyPhone" maxlength="20"></td>' +'<td>' +'<input id="dDentifySucessDate'+num+'" type="text" name="personFamilyInfoList['+num+'].DDentifySucessDate" onfocus="WdatePicker()" notnull="true" vdisp="首次參保時間" size=" 18" maxlength="18"/><font color="red">*</font></td>' +'<td>' +'<input id="dDentifyLostDate'+num+'" type="text " name="personFamilyInfoList['+num+'].DDentifyLostDate" onfocus="WdatePicker()" notnull="true" vdisp="首次參保時間" size="18" maxlength="18"/></td> ' +'<td>' +'<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 style="font-family:KaiTi_GB2312;font-size:14px;"></script></ span>
後台添加方法:
<span style="font-family:KaiTi_GB2312;font-size:14px;">/** * <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 style="font-family:KaiTi_GB2312;font-size:14px;">/** * <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 style="font-family:KaiTi_GB2312;font-size:14px;">$(function(){
loadCheck();
});</span>
它就是如下代碼的縮寫:
複製代碼代碼如下:<span style="font-family:KaiTi_GB2312;font-size:14px;"> $(document).ready(function(){
loadCheck();
});</span>
其實這個代碼和下面的代碼是一個意思:
複製代碼代碼如下:<span style="font-family:KaiTi_GB2312;font-size:14px;">window.onload=function(){
loadCheck();
}</span>
這樣就不用在body的onload事件裡面調用,只需要在js程序段裡面編寫就可以了。雖然下面的代碼可以和上面兩個互換,但他們之間又有不同。首先是執行時間不同,$(document).ready在頁面框架下載完畢後就執行,而window.onload必須在頁面全部加載完畢(包含圖片下載)後才能執行。很明顯前者的執行效率高於後者。再就是執行數量的不同,$(document).ready可以重複寫多個,而且每次執行結果不同;而window.onload儘管可以執行多個,但僅輸出最後一個執行結果,無法完成多個結果的輸出。
以上就是本文的全部內容,希望對大家的學習有所幫助。