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");
}
});
}
Jsp&Servlet入门级项目全程实录第7讲的更多相关文章
- Jsp&Servlet入门级项目全程实录第1讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.jdbc数据库连接(略) 2.登录表单 2.1设置内边距 <dir style="padding- ...
- Jsp&Servlet入门级项目全程实录第8讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.添加dao public int studentAdd(Connection con,Student studen ...
- Jsp&Servlet入门级项目全程实录第4讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.添加搜索.添加.修改.删除按钮 <div id="tb"> <div> ...
- Jsp&Servlet入门级项目全程实录第3讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.建立数据表及数据(略) 2.装载驱动,建立数据表 <link rel="stylesheet&qu ...
- Jsp&Servlet入门级项目全程实录第2讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.导入jquery-easyui-1.3.3包( http://www.jeasyui.com/) 2.在页面导入e ...
- Jsp&Servlet入门级项目全程实录第6讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.建立数据表及数据(略) 2.创建student model package com.java1234.model; ...
- Jsp&Servlet入门级项目全程实录第5讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.修改功能实现 dao public int gradeAdd(Connection con,Grade grade ...
- JSP/Servlet Web应用中.properties文件的放置与读取
本地项目 在本地类库中,我经常使用当前目录来放置.properties文件,这时调用方只要引用我的jar,并且将我的.properties放在他的classpath里面即可,比如: p.load(ne ...
- 创建jsp+Servlet+JavaBean+JDBC+MySQL项目的过程
1 根据需求建立Mysql数据,确立数据库的表的字段.属性.主键,外键等.下面我使用的数据库名dev ,表名user,字段 name,设置为主键.用户名不能为空,字段password,密码 2 在E ...
随机推荐
- Python 基础入门
最近业余时间看看Python,从网上找找一些语法看看 http://www.runoob.com/python/python-tutorial.html IDE工具:https://www.pytho ...
- EF动态linq的两种方式
网上收集的资源 我怕遗忘就在自己博客记录下,有些我忘记原文地址了请见谅 这个链接的动态sql方式是 where("c=>c.id==id") https://weblogs ...
- JVM内存回收区域+对象存活的判断+引用类型+垃圾回收线程
此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 注意:本文主要参考自<深入理解Java虚拟机(第二版)> 说明:查看本文之前,推荐先知道JVM ...
- Topological Sor-207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- hdoj1373 Channel Allocation(极大团)
题意是有若干个接收器,给出每个接收器的相邻接收器.相邻的接收器不能使用同一信号频道.问所需要的信号频道数. 求该无向图的极大团. #include<iostream> #include&l ...
- 对一致性hash原理的理解
一致性hash算法解决的核心问题是,当solt数发生变化的时候能够尽量少的移动数据.该算法最早在<Consistent Hashing and Random Trees:Distributed ...
- C语言的组成 以及预编译
这么多年过去了,回头再来学习一下C语言,发现很多不一样的感觉 #include <stdio.h> int main(int argc, const char * argv[]) { pr ...
- SecurityManager入门
java安全管理器SecurityManager入门 SecurityManager 每个Java应用都可以有自己的安全管理器,它是防范恶意攻击的主要安全卫士. 安全管理器通过执行运行阶段检查和访问授 ...
- centos7无法上网问题
在虚拟机VM中安装了centos7,无法上网,在同一个虚拟机里,也安装了Ubuntu是可以上网的,不知道咋回事,所以上网查了资料博客,现总结如下. 一.首先打开虚拟的设置,可以看到虚拟机网络的设置默认 ...
- oracle case when 语句的用法详解
1. CASE WHEN 表达式有两种形式 复制代码代码如下: --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他 ...