ileson 发表于 2013-2-7 15:29:03

form表单 批量新增

基本上网上全是转自他的文章:呵呵备忘!

http://www.blogjava.net/max/archive/2006/12/08/86439.html

自己写个不绑定form的。。。

也就是自己封装数据

直接代码:

xxx.html
<html><head><script   language="javascript">       var count= 0 ; //全局变量统计总数      var maxfile = 5;//限制控件个数   //增加元素    function addmx() {          //if(count >= maxfile)    return;//限制最多显示多少个行      count++;         //自增id不同的HTML对象,并附加到容器最后      var newDiv ="<div id=divStock" + count +">"            +"<table align=center width=60% border=1><tr>"         +"<td>姓名:</td><td><input name=emp["+count+"].name size=4></td>"         +"<td>性别:</td><td><input name=emp["+count+"].sex size=4></td>"         +"<td>年龄:</td><td><input name=emp["+count+"].age size=4></td>"+"<td>"+"<a href=javascript:delrow('divStock" + count + "');>删除</a>"+"</td>"+"</tr>"            +"</table>"            +"</div>";             document.getElementById("coll").insertAdjacentHTML("beforeEnd", newDiv);         }       //删除指定元素    function delrow(diva) {          count--;         document.getElementById(diva).parentNode.removeChild(document.getElementById(diva));       }    function sub() {document.form1.action="xxx.do?total="+count;//servlet要取得一共增了多少。      document.form1.submit();       }   </script></head><body><a href="#" id="">新增页面控件</a><form action="" name="form1" method=""><div id="coll"></div><input type="submit" value="submit"/></form></body></html>
action.java
int count=Integer.parseInt(request.getParameter("total"););List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();while(count>0){Map<String, Object> map=new LinkedHashMap<String, Object>();String eName=request.getParameter("emp["+count+"].name");String eSex=request.getParameter("emp["+count+"].sex");String eAge=request.getParameter("emp["+count+"].age");map.put("name", eName);map.put("sex", eSex);map.put("age", eAge);count--;list.add(map);//把一个一个的Map放到list中去}
dao.java
public void saveProdConfigure(Connection conn,List<Map<String, Object>> list) {String sql="insert into t_prod_configure (USER_NAME,USER_SEX,USER_AGE) values(?,?,?)";PreparedStatement ps=null;try {ps = conn.prepareStatement(sql);for(int i=0;i<list.size();i++){ps.setString(1, list.get(i).get("name").toString());//第一个字段ps.setString(2, list.get(i).get("sex").toString());//第二个字段赋值ps.setString(3, list.get(i).get("age").toString());ps.addBatch();}ps.executeBatch();} catch (SQLException e1) {e1.printStackTrace();}}
页: [1]
查看完整版本: form表单 批量新增