增删改接口
/**
* 增删改通用
* @param sql
* @param obj
*/
public static void executeUpdate(String sql,Object[] obj) {
Connection conn=null;
PreparedStatement stmt=null;
try {
conn=DBHelper.getConnection();
stmt=conn.prepareStatement(sql);
ParameterMetaData params = stmt.getParameterMetaData();
for (int i = 0; i < params.getParameterCount(); i++) {
stmt.setObject(i+1, obj[i]);
}
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(conn, stmt, null);
}
}
public void addBook(Book book) {
String sql="insert into t_book_vue(bookname,price,booktype) values(?,?,?)";
super.executeUpdate(sql, new Object[] {
book.getBookname(),
book.getPrice(),
book.getBooktype()
});
}
public void editBook(Book book) {
String sql="update t_book_vue set bookname=?,price=?,booktype=? where id=?";
super.executeUpdate(sql, new Object[] {
book.getBookname(),
book.getPrice(),
book.getBooktype(),
book.getId()
});
}
public void delBook(Book book) {
String sql="delete from t_book_vue where id=?";
super.executeUpdate(sql, new Object[] {
book.getId()
});
}
Map<String, Object> json = new HashMap<String, Object>();
public String queryBookPager(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
List<Book> books = bookDao.queryBookPager(book, pageBean);
Map<String,Object> map=new HashMap<String,Object>();
Map<String,Object> data=new HashMap<String,Object>();
data.put("rows", books);
data.put("total", pageBean.getTotal());
map.put("data", data);
map.put("success", true);
map.put("msg", "OK");
mapper.writeValue(resp.getOutputStream(),map);
return null;
}
public String addBook(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
try {
bookDao.addBook(book);
json.put("msg", "新增书本信息成功");
json.put("success", true);
mapper.writeValue(resp.getOutputStream(),json);
} catch (Exception e) {
json.put("msg", "新增书本信息失败");
json.put("success", false);
mapper.writeValue(resp.getOutputStream(),json);
}
return null;
}
public String editBook(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
try {
bookDao.editBook(book);
json.put("msg", "修改书本信息成功");
json.put("success", true);
mapper.writeValue(resp.getOutputStream(),json);
} catch (Exception e) {
json.put("msg", "修改书本信息失败");
json.put("success", false);
mapper.writeValue(resp.getOutputStream(),json);
}