实例讲解Java批量插入、更新数据
程序员文章站
2024-03-06 10:28:25
java的批量添加数据,多个字段同时添加多条数据,我不知道你遇到过没有。今天我们就以一个具体的实例来说一下java的批量添加数据,面向的是oracle数据库。
前台页...
java的批量添加数据,多个字段同时添加多条数据,我不知道你遇到过没有。今天我们就以一个具体的实例来说一下java的批量添加数据,面向的是oracle数据库。
前台页面:
<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">基本信息</div> <div id="semsinfo" onclick="activetabpanel(2)" style="margin-right:2px;" class="tab1" >页签1</div> <div id="changeinfo" onclick="activetabpanel(3)"style="margin-right:2px;" class="tab1" >页签2</div> <div id="familyinfo" onclick="activetabpanel(4)" style="margin-right:2px;" class="tab1" >页签3</div> <div id="changeinfo" onclick="activetabpanel(5)"style="margin-right:2px;" class="tab1" >页签4</div> <div id="familyinfo" onclick="activetabpanel(6)" style="margin-right:2px;" class="tab1" >页签5</div> <div id="familyinfo" onclick="activetabpanel(7)" style="margin-right:2px;" 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" style="width:70px;"/> <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" style="width:70px;"/> </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 style="width:100%"> <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 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 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" 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="首次参保时间" class="content_content_input" size="18" maxlength="18" style="width:70px;"/></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 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>
loadcheck();
});</span>
它就是如下代码的缩写:
复制代码 代码如下:
<span style="font-family:kaiti_gb2312;font-size:14px;"> $(document).ready(function(){
loadcheck();
});</span>
loadcheck();
});</span>
其实这个代码和下面的代码是一个意思:
复制代码 代码如下:
<span style="font-family:kaiti_gb2312;font-size:14px;">window.onload=function(){
loadcheck();
}</span>
loadcheck();
}</span>
这样就不用在body的onload事件里面调用,只需要在js程序段里面编写就可以了。虽然下面的代码可以和上面两个互换,但他们之间又有不同。首先是执行时间不同,$(document).ready在页面框架下载完毕后就执行,而window.onload必须在页面全部加载完毕(包含图片下载)后才能执行。很明显前者的执行效率高于后者。再就是执行数量的不同,$(document).ready可以重复写多个,而且每次执行结果不同;而window.onload尽管可以执行多个,但仅输出最后一个执行结果,无法完成多个结果的输出。
以上就是本文的全部内容,希望对大家的学习有所帮助。
推荐阅读
-
实例讲解Java批量插入、更新数据
-
Java数据结构及算法实例:插入排序 Insertion Sort
-
实例讲解Java批量插入、更新数据
-
Java数据结构及算法实例:插入排序 Insertion Sort
-
MyBatis批量插入数据到Oracle数据库中的两种方式(实例代码)
-
MyBatis批量插入数据到Oracle数据库中的两种方式(实例代码)
-
sqlite数据库的介绍与java操作sqlite的实例讲解
-
Java自动化测试中多数据源的切换(实例讲解)
-
sqlite数据库的介绍与java操作sqlite的实例讲解
-
php+mysqli实现批量执行插入、更新及删除数据的方法,phpmysqli