1.后台开发
dao
@Override
public void updateBook(Book book) {
Connection con=null;
PreparedStatement ps=null;
try {
String sql="update t_book set bookname=?,price=?,booktype=? where id= ?";
con=DBHerpr.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, book.getBook_name());
ps.setObject(2, book.getBook_price());
ps.setString(3, book.getBook_type());
ps.setInt(4, book.getId());
int n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpr.myClose(con, ps, null);
}
}
@Override
public void deleteBook(int id) {
Connection con=null;
PreparedStatement ps=null;
try {
String sql="delete from t_book where id="+id;
con=DBHerpr.getCon();
ps=con.prepareStatement(sql);
int n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpr.myClose(con, ps, null);
}
}
serlvie
@Override
public void deleteBook(Book book) {
dao.deleteBook(book);
}
@Override
public void deleteBook(int id) {
dao.deleteBook(id);
}
2.修改功能
function openDialog(row) {
let title="增加书本";
let action="/bookAddServlet";
if(row){
title="修改书本信息";
action="/bookupdate";
}
$('#editDialog').dialog({
title: title,
width: 300,
height: 260,
closed: false,
cache: false,
href: 'editBook.jsp',
modal: true,
buttons:[{
text:'保存',
iconCls:"icon-save",
handler:function(){
$.ajax({
url:ctx + action,
type:'post',
data:$("#bookForm").serialize(),
dataType:'json',
success:function(resp){
if(resp.success){
$.messager.alert('通知','操作成功');
$("#editDialog").dialog("close");
qry();
}else{
$.messager.alert('警告','出现异常请联系管理员');
}
}
});
}
},{
text:'关闭',
handler:function(){
}
}],
onLoad:function() {
if(row) {
$("#bookForm").form("reset");
$("#bookForm").form("load",row);
}
}
});
}
3.删除功能
$("#deleteBook").click(function(){
let row =$("#dg").datagrid("getSelected");
if(!row){
$.messager.alert('询问','请选择要删除的对象');
return;
}
$.messager.confirm('确定','您确定删除',function(r){
if(r){
let id =row.id;
$.ajax({
url:ctx + "/bookdelete",
type:'post',
data:{
id:id
},
dataType:'json',
success:function(resp){
if(resp.success){
$.messager.alert('通知','操作成功');
qry();
}else{
$.messager.alert('警告','出现异常请联系管理员');
}
}
});
}
});
});