惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!
1、添加dao public int studentAdd(Connection con,Student student)throws Exception{ String sql="insert into t_student values(null,?,?,?,?,?,?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, student.getStuNo()); pstmt.setString(2, student.getStuName()); pstmt.setString(3, student.getSex()); pstmt.setString(4, DateUtil.formatDate(student.getBirthday(), "yyyy-MM-dd")); pstmt.setInt(5, student.getGradeId()); pstmt.setString(6, student.getEmail()); pstmt.setString(7, student.getStuDesc()); return pstmt.executeUpdate(); } 2、修改dao public int studentModify(Connection con,Student student)throws Exception{ String sql="update t_student set stuNo=?,stuName=?,sex=?,birthday=?,gradeId=?,email=?,stuDesc=? where stuId=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, student.getStuNo()); pstmt.setString(2, student.getStuName()); pstmt.setString(3, student.getSex()); pstmt.setString(4, DateUtil.formatDate(student.getBirthday(), "yyyy-MM-dd")); pstmt.setInt(5, student.getGradeId()); pstmt.setString(6, student.getEmail()); pstmt.setString(7, student.getStuDesc()); pstmt.setInt(8, student.getStuId()); return pstmt.executeUpdate(); } 3、修改servlet request.setCharacterEncoding("utf-8"); String stuNo=request.getParameter("stuNo"); String stuName=request.getParameter("stuName"); String sex=request.getParameter("sex"); String birthday=request.getParameter("birthday"); String gradeId=request.getParameter("gradeId"); String email=request.getParameter("email"); String stuDesc=request.getParameter("stuDesc"); String stuId=request.getParameter("stuId"); Student student=null; try { student=new Student( stuNo, stuName, sex, DateUtil.formatString(birthday, "yyyy-MM-dd"), Integer.parseInt(gradeId), email, stuDesc); } catch (Exception e) { e.printStackTrace(); } if(StringUtil.isNotEmpty(stuId)){ student.setStuId(Integer.parseInt(stuId)); } Connection con=null; try { con=dbUtil.getCon(); int saveNums=0; JSONObject result=new JSONObject(); if(StringUtil.isNotEmpty(stuId)){ saveNums=studentDao.studentModify(con, student); }else{ saveNums=studentDao.studentAdd(con, student); } if(saveNums>0){ result.put("success", "true"); }else{ result.put("success", "true"); result.put("errorMeg", "删除失败"); } ResponseUtil.write(response, result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } 封装student public Student(String stuNo, String stuName, String sex, Date date, int gradeId, String email, String stuDesc) { super(); this.stuNo = stuNo; this.stuName = stuName; this.sex = sex; this.birthday = date; this.gradeId = gradeId; this.email = email; this.stuDesc = stuDesc; } 字符串转日期 public static Date formatString(String str,String format) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat(format); return sdf.parse(str); } 日期转字符串 public static String formatDate(Date date,String format){ String result=""; SimpleDateFormat sdf=new SimpleDateFormat(format); if(date!=null){ result=sdf.format(date); } return result; } 4、修改web.xml <servlet> <servlet-name>studentSaveServlet</servlet-name> <servlet-class>com.java1234.web.StudentSaveServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>studentSaveServlet</servlet-name> <url-pattern>/studentSave</url-pattern> </servlet-mapping> 5、前端(略) 6、email验证 validType="email" 6、隐藏列 <th field="gradeId" width="100" align="center" hidden="true">班级ID</th> 7、重置 function resetValue(){ $('#stuNo').val(""); $('#stuName').val(""); $('#sex').combobox("setValue",""); $('#bbirthday').datebox("setValue",""); $('#gradeId').combobox("setValue",""); $('#email').val(""); $('#stuDesc').val(""); } 8、关闭 function closeStudentDialog() { $("#dlg").dialog("close"); resetValue(); } 9、添加 function saveStudent(){ $("#fm").form("submit",{ url:url, onSubmit:function(){ if($('#sex').combobox("getValue")==""){ $.messager.alert("系统提示","请选择性别"); return false; } if($('#gradeId').combobox("getValue")==""){ $.messager.alert("系统提示","请选择所属班级"); return false; } return $(this).form("validate"); }, success:function(result){ if(result.errorMsg){ $.messager.alert("系统提示",result.errorMsg); return; }else{ $.messager.alert("系统提示","保存成功"); resetValue(); $("#dlg").dialog("close"); $("#dg").datagrid("reload"); } } }); } 10、修改前端 function openStudentModifyDialog(){ var selectedRows=$("#dg").datagrid('getSelections'); if(selectedRows.length!=1){ $.messager.alert("系统提示","请选择一条要编辑的数据!"); return; } var row=selectedRows[0]; $("#dlg").dialog("open").dialog("setTitle","编辑学生信息"); $("#fm").form("load",row); url="studentSave?stuId="+row.stuId; } 11、关联判断操作 在删除班级时判断班级下面是否有人? public boolean getStudentByGradeId(Connection con,String gradeId)throws Exception{ String sql="select * from t_student where gradeId=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, gradeId); ResultSet rs=pstmt.executeQuery(); if(rs.next()){ return true; }else{ return false; } } String str[]=delIds.split(","); for(int i=0;i<str.length;i++){ boolean f=studentDao.getStudentByGradeId(con, str[i]); if(f){ result.put("errorIndex", i); result.put("errorMsg", "班级下面有学生,不能删除!"); ResponseUtil.write(response, result); return; } } function deleteGrade(){ var selectedRows=$("#dg").datagrid('getSelections'); if(selectedRows.length==0){ $.messager.alert("系统提示","请选择要删除的数据!"); return; } var strIds=[]; for(var i=0;i<selectedRows.length;i++){ strIds.push(selectedRows[i].id); } var ids=strIds.join(","); $.messager.confirm("系统提示","您确认要删掉这<font color=red>"+selectedRows.length+"</font>条数据吗?",function(r){ if(r){ $.post("gradeDelete",{delIds:ids},function(result){ if(result.success){ $.messager.alert("系统提示","您已成功删除<font color=red>"+result.delNum+"</font>条数据!"); $("#dg").datagrid("reload"); }else{ $.messager.alert("系统提示",'<font color=red>'+selectedRows[result.errorIndex].gradeName+'</font>'+result.errorMsg); } },"json"); } }); }