Jsp&Servlet入门级项目全程实录第7讲

惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!

1、获取搜索条件值
function searchStudent(){
$('#dg').datagrid('load',{
stuNo:$('#s_stuNo').val(),
stuName:$('#s_stuName').val(),
sex:$('#s_sex').combobox("getValue"),
bbirthday:$('#s_bbirthday').datebox("getValue"),
ebirthday:$('#s_ebirthday').datebox("getValue"),
gradeId:$('#s_gradeId').combobox("getValue")
});
} 2、servlet
String stuNo=request.getParameter("stuNo");
String stuName=request.getParameter("stuName");
String sex=request.getParameter("sex");
String bbirthday=request.getParameter("bbirthday");
String ebirthday=request.getParameter("ebirthday");
String gradeId=request.getParameter("gradeId"); Student student= new Student();
if(stuNo!=null){
student.setStuNo(stuNo);
student.setStuName(stuName);
student.setSex(sex);
if(StringUtil.isNotEmpty(gradeId)){
student.setGradeId(Integer.parseInt(gradeId));
}
}
//获取page、rows请求
String page=request.getParameter("page");
String rows=request.getParameter("rows"); //封装
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows)); Connection con=null;
try {
con=dbUtil.getCon();
JSONObject result=new JSONObject();
JSONArray jsonArray=JsonUtil.formatRsToJsonArray(studentDao.studentList(con, pageBean,student,bbirthday,ebirthday));
int total=studentDao.studentCount(con,student,bbirthday,ebirthday);
result.put("rows", jsonArray);
result.put("total", total);
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();
}
} 3、查询dao
public ResultSet studentList(Connection con,PageBean pageBean,Student student,String bbirthday,String ebirthday)throws Exception{
StringBuffer sb=new StringBuffer("select * from t_student s,t_grade g where s.gradeId=g.id");
if(StringUtil.isNotEmpty(student.getStuNo())){
sb.append(" and s.stuNo like '%"+student.getStuNo()+"%'");
}
if(StringUtil.isNotEmpty(student.getStuName())){
sb.append(" and s.StuName like '%"+student.getStuName()+"%'");
}
if(StringUtil.isNotEmpty(student.getSex())){
sb.append(" and s.sex ='"+student.getSex()+"'");
}
if(student.getGradeId()!=-1){
sb.append(" and s.GradeId ='"+student.getGradeId()+"'");
}
if(StringUtil.isNotEmpty(bbirthday)){
sb.append(" and TO_DAYS(s.birthday)>=TO_DAYS('"+bbirthday+"')");
}
if(StringUtil.isNotEmpty(ebirthday)){
sb.append(" and TO_DAYS(s.birthday)<=TO_DAYS('"+ebirthday+"')");
} if(pageBean!=null){
sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
} public int studentCount(Connection con,Student student,String bbirthday,String ebirthday)throws Exception{
StringBuffer sb=new StringBuffer("select count(*) as total from t_student s,t_grade g where s.gradeId=g.id");
if(StringUtil.isNotEmpty(student.getStuNo())){
sb.append(" and s.stuNo like '%"+student.getStuNo()+"%'");
}
if(StringUtil.isNotEmpty(student.getStuName())){
sb.append(" and s.StuName like '%"+student.getStuName()+"%'");
}
if(StringUtil.isNotEmpty(student.getSex())){
sb.append(" and s.sex ='"+student.getSex()+"'");
}
if(student.getGradeId()!=-1){
sb.append(" and s.GradeId ='"+student.getGradeId()+"'");
}
if(StringUtil.isNotEmpty(bbirthday)){
sb.append(" and TO_DAYS(s.birthday)>=TO_DAYS('"+bbirthday+"')");
}
if(StringUtil.isNotEmpty(ebirthday)){
sb.append(" and TO_DAYS(s.birthday)<=TO_DAYS('"+ebirthday+"')");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
return rs.getInt("total");
}else{
return 0;
}
} 4、删除dao
public int studentDelect(Connection con,String delIds)throws Exception{
String sql="delete from t_student where stuId in("+delIds+")";
PreparedStatement pstmt=con.prepareStatement(sql);
return pstmt.executeUpdate();
} 5、删除servlet
String delIds=request.getParameter("delIds"); Connection con=null;
try {
con=dbUtil.getCon();
JSONObject result=new JSONObject();
int delNums=studentDao.studentDelect(con, delIds);
if(delNums>0){
result.put("success", "true");
result.put("delNums", delNums);
}else{
result.put("errorMeg", "删除失败");
}
result.put("delNum", delNums);
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();
}
} 6、修改web.xml
<servlet>
<servlet-name>studentDeleteServlet</servlet-name>
<servlet-class>com.java1234.web.StudentDeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentDeleteServlet</servlet-name>
<url-pattern>/studentDelete</url-pattern>
</servlet-mapping> 7、删除前端
function deleteStudent(){
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].stuId);
}
var ids=strIds.join(",");
$.messager.confirm("系统提示","您确认要删掉这<font color=red>"+selectedRows.length+"</font>条数据吗?",function(r){
if(r){
$.post("studentDelete",{delIds:ids},function(result){
if(result.success){
$.messager.alert("系统提示","您已成功删除<font color=red>"+result.delNum+"</font>条数据!");
$("#dg").datagrid("reload");
}else{
$.messager.alert("系统提示",result.errorMsg);
}
},"json");
}
});
}
上一篇:eclipse如何为java项目生成API文档、JavaDoc


下一篇:HTML中特殊字符和与之对应的ASCII代码