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 ...
随机推荐
- .NET Core 运行时标识符 (RID) 目录
RID 是什么? RID 是运行时标识符的缩写. RID 用于标识其中将运行应用程序或资产(即程序集)的目标操作系统. 其外观类似如下:“ubuntu.14.04-x64”.“win7-x64”.“o ...
- NetCore入门篇:(三)Net Core项目Nuget及Bower包管理
一.创建项目 1.如何创建项目,参照上一篇文章 二.程序包介绍 1.Net Core的程序包分前后端两种,后端用nuget,前端用bower. 2.与Net 不同,Net Core引用nuget包时, ...
- SSE sqrt还是比C math库的sqrtf快了不少
#include <stdio.h> #include <xmmintrin.h> #define NOMINMAX #include <windows.h> #i ...
- From Alpha to Gamma (II)
这篇文章被拖延得这么久是因为我没有找到合适的引言 -- XXX 这一篇接着讲Gamma.近几年基于物理的渲染(Physically Based Shading, 后文简称PBS)开始在游戏业界受到关注 ...
- Restframework 频率throttle组件实例-3
频率逻辑: from rest_framework.throttling import BaseThrottle import time VISIT_RECORD={} class VisitThro ...
- 简单版nginx lua 完成流量上报于中间件
本文链接:https://www.cnblogs.com/zhenghongxin/p/9131226.html 公司某些业务下,需要将请求的流量上报于中间件(kafka,rabbitMq等),让st ...
- js string 和 json 互转
var o = JSON.parse('{"a": 8}'); JSON. stringify(o);
- centos6安装最新syslog-ng推送hdfs
可参考以下网址: installhttps://www.syslog-ng.com/community/b/blog/posts/latest-syslog-ng-available-rhel-6-c ...
- iOS 代码混淆的简单使用
1.工具下载 http://stevenygard.com/projects/class-dump/ 选择dmg安装包 2.打开终端输入:open/usr/local/bin 3. 4.修改权限在 ...
- 【10】JMicro微服务-API网关
如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 往下看前,建议完成前面1到9小节 1. Api网关基本特性: Api网关作为对外网提供服务的基本入口,地位类似于NGINX, ...