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 ...
随机推荐
- Asp.NetCore初步探究
1, 新建一个空的AspNetCore项目,默认Program下的代码如下: public static void Main(string[] args) { BuildWebHost(args ...
- 毕业回馈-89C51之数码管的使用
7段码的数码管由7个LED等共同组成,根据公共端的不同有共阴和共阳之分.现在很多数码管在7段码的基础上加了一个.即dp,其内部结构如下图所示: 公共端为LED灯的阴极,所以为共阴极接法: 公共端为阳极 ...
- 网易郑栋:数据采集与分析的那些事——从数据埋点到AB测试
本文由 网易云发布. 4月8日晚,DTalk邀请到了网易互联网分析产品.可视化 BI 产品的负责人—郑栋老师,进行了一次关于<网易郑栋:数据采集与分析的那些事第一弹: 数据篇>的主题分享 ...
- windows下redis安装及应用
一.下载安装Redis(windows版本) 1.下载地址:https://github.com/MicrosoftArchive/redis/releases 2.安装: 1)打开运行窗口,输出cm ...
- Map-560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- class字节码结构(四)(方法集合的结构)
<Java虚拟机原理图解>1.5. class文件中的方法表集合--method方法在class文件中是怎样组织的 一个类有多个方法,所以方法肯定是一个集合. 目标是: 1,了解方法在字节 ...
- rabbitmq系列三 之发布/订阅
1.发布/订阅 在上篇教程中,我们搭建了一个工作队列,每个任务只分发给一个工作者(worker).在本篇教程中,我们要做的跟之前完全不一样 —— 分发一个消息给多个消费者(consumers).这种模 ...
- An assembly specified in the application dependencies manifest
.Net Core 运行的时候报错 An assembly specified in the application dependencies manifest (xxx.deps.json) was ...
- 修改WAMPServer(Apache+PHP+MySQL一键式安装)中mysql默认空密码
Note:在EclipsePHP中配置WorkSpace时,将工作目录指到执行PHP代码的www目录下 ,便于在Eclipse下编写PHP项目 eg:D:\KelvinSoftwar ...
- MySQl资料链接
原文:http://www.uml.org.cn/sjjm/sjjm-MySql.asp MySQl MySQL高可用数据库内核深度优化的四重定制 MySQL数据表存储引擎类型及特性 My ...