实现图书的删除/修改
一、图书的删除
1.删除的方法
@Override
public void delBooks(int bid) {
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from tb_book where bid = "+ bid;
try {
//获取连接
conn = DBHelper.getConn();
//传入执行对象
ps = conn.prepareStatement(sql);
//返回所影响的行数
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(conn, ps, null);
}
}
2.编写servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
//通过request获取前端传递过来的图书编号
Integer bid = request.getParameter("bid")!=null?Integer.valueOf(request.getParameter("bid")):0;
//实例化业务逻辑层
IBooksBiz ibb = new BooksBizImpl();
//Map集合
Map<String,Object> maps = new HashMap<String,Object>();
//调用删除的方法
try {
ibb.delBooks(bid);
maps.put("message", true);
} catch (Exception e) {
maps.put("message", false);
}
//JSON工具
ObjectMapper mapper = new ObjectMapper();
String writeValueAsString = mapper.writeValueAsString(maps);
//out对象
PrintWriter out = response.getWriter();
out.write(writeValueAsString);
out.flush();
out.close();
}
}
//删除的点击事件
$("#delBookId").click(function(){
//1.获取选中的行 getSelected方法 获取是否选中行
let row = $('#bookListID').datagrid("getSelected");
//console.log(row);
if(!row){//空 没有选中
$.messager.alert('信息',"请选中行再操作");
return;
}
//动用jQuery提供的ajax方法 id丢过去 id row.bid
//DelBook.do
$.post(xPath+"/DelBook.do",{"bid":row.bid},function(data){
if(data.message){
$.messager.alert('信息',"删除成功");
//重新加载
mydemo();
}
});
});
})
二、图书修改
1、修改的方法
@Override
public void editBooks(int bid, Books book) {
Connection conn = null;
PreparedStatement ps = null;
String sql = "update tb_book set bname = ? , bprice = ? , btype = ? where bid = "+bid;
try {
//获取连接
conn = DBHelper.getConn();
//传入执行对象
ps = conn.prepareStatement(sql);
//占位符赋值
ps.setString(1, book.getBname());
ps.setFloat(2, book.getBprice());
ps.setString(3, book.getBtype());
//返回所影响的行数
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(conn, ps, null);
}
}
增删改所需的辅助工具类
Result.java
package com.tang.util;
/**
* (响应结果集)
* @author Administrator
*
* @param <T>
*/
public class Result<T> {
private int code;
private String msg;
private T data;
public static Result SUCCESS = new Result<>(200,"操作成功");
public static <T> Result ok(T data) {
return new Result(data);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
private Result(int code, String msg) {
super();
this.code = code;
this.msg = msg;
}
private Result() {
super();
}
private Result(T data) {
super();
this.data = data;
}
}
BookDao.java
package com.tang.dao;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import com.tang.entity.Book;
import com.tang.util.BaseDao;
import com.tang.util.PageBean;
import com.tang.util.PinYinUtil;
import com.tang.util.StringUtils;
public class BookDao extends BaseDao<Book> {
public List<Book> list(Book book,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String name = book.getName();
String sql = "select * from t_easyui_book where true";
if(StringUtils.isNotBlank(name)) {
sql += " and name like '%"+name+"%'";
}
return super.executeQuery(sql, Book.class, pageBean);
}
//增加
public int add(Book book) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
// pinyin的属性值不是从jsp传递过来的
book.setPinyin(PinYinUtil.getAllPingYin(book.getName()));
// 从前端jsp传到后端的deployTime属性值是String类型的,而数据库需要的是date/Timestamp
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sql = "insert into t_easyui_book values(null,?,?,?,?,?,?,?,?,?,?,?)";
return super.executeUpdate(sql, book, new String[] {"name","pinyin","cid","author","price","image","publishing","description","state","deployTime","sales"});
}
//修改
public int edit(Book book) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
book.setPinyin(PinYinUtil.getAllPingYin(book.getName()));
String sql = "update t_easyui_book set name=?,pinyin=?,cid=?,author=?,price=?,image=?,publishing=?,description=?,state=?,deployTime=?,sales=? where id=?";
return super.executeUpdate(sql, book, new String[] {"name","pinyin","cid","author","price","image","publishing","description","state","deployTime","sales","id"});
}
//修改
public int del(Book book) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "delete from t_easyui_book where id=?";
return super.executeUpdate(sql, book, new String[] {"id"});
}
}
③editBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 通过指令进行引入 -->
<!-- 在此处引用的原因是因为此处能作用script标签定义的变量 -->
<%@ include file="static/common/NewFile1.jsp" %>
</head>
<body>
<!-- 弹窗式表单 -->
<!-- 表单显示区域 -->
<div style="text-align:center">
<form id="bookForm" >
<!-- 隐藏域:将数据传递到servlet,但不需要显示 -->
<div style="display:none">
<!-- 注意:表单的name属性名字(name属性)如需要传递到servlet与数据库的字段名最好一致,这是EasyUI的一些功能使用的规范 -->
<label for="name">书籍编号:</label>
<input class="easyui-validatebox" style="margin:15px auto;" type="text" name="bid" data-options="required:true" />
</div>
<div>
<label for="name">书籍名称:</label>
<input class="easyui-validatebox" style="margin:15px auto;" type="text" name="bname" data-options="required:true" />
</div>
<div>
<label for="name">书籍价格:</label>
<input class="easyui-validatebox" style="margin:15px auto;" type="text" name="bprice" data-options="required:true" />
</div>
<div>
<label for="name">书籍类型:</label>
<input class="easyui-validatebox" style="margin:15px auto;" type="text" name="btype" data-options="required:true" />
</div>
</form>
</div>
</body>
</html>