一、商品管理
1.查询案例演示
之前我们编辑已经写完,就只需要完善增删查
list.jsp
查找
输入商品信息查找到对应的图书(这里我只写由编号查图书,因为编号是唯一的,完善代码可以自己添加)
2.实现步骤
2.1 products/list.jsp
2.1 findProductByIdServlet
这里后面我们就用新版自带的路径/findProductByIdServlet
//@WebServlet(name = "findProductByIdServlet", value = "/findProductByIdServlet")
//商品查找
if("select".equals(act)){
String id=request.getParameter("id");
int sid = Integer.parseInt(id);
Books b=new Books();
b.setId(sid);
BooksDao bd=new BooksDao();
List<Books> books=new ArrayList<Books>();
books=bd.selecter(b);
request.getSession().setAttribute("ps",books);
request.getRequestDispatcher("/admin/products/list.jsp").forward(request,response);
}
2.1 BooksDao
//商品查询
public List selecter(Books book){
List<Books> list=new ArrayList<Books>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Books books=new Books();
try {
con = JDBCutils.getConnection();
String sql="select *from books where id=?";
pst=con.prepareStatement(sql);
pst.setInt(1,book.getId());
rs=pst.executeQuery();
if(rs.next()){
books.setId(rs.getInt("id"));
books.setName(rs.getString("name"));
books.setPrice(rs.getDouble("price"));
books.setCatagory(rs.getString("catagory"));
books.setNum(rs.getInt("num"));
books.setDescription(rs.getString("description"));
books.setImgurl(rs.getString("imgurl"));
list.add(books);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
二、商品管理
1.添加案例演示
点击添加,进入添加页面,填写数据点击确定
add.jsp
发现已经添加
2.实现步骤
2.1 add.jsp
2.2 findProductByIdServlet
//商品添加
if("add".equals(act)){
SmartUpload su=new SmartUpload();
su.initialize(getServletConfig(),request,response);
Books books=new Books();
try {
su.upload();
//生成上传文件名
books.setName(su.getRequest().getParameter("name"));
books.setPrice(Double.parseDouble(su.getRequest().getParameter("price")));
books.setNum(Integer.parseInt(su.getRequest().getParameter("num")));
books.setCatagory(su.getRequest().getParameter("catagory"));
books.setDescription(su.getRequest().getParameter("description"));
IPTimeStamp its=new IPTimeStamp(request.getRemoteAddr());
//获取扩展名(jpg)
String ext=su.getFiles().getFile(0).getFileExt();
//ip +时间戳+字符串
String fileName =its.getIpTimeRand()+"."+ext;
//路径
String pathName=getServletContext().getRealPath("/")+"upload"+ File.separator+fileName;
//保存文件
su.getFiles().getFile(0).saveAs(pathName);
//设置商品图片地址列为文件的完整路径
books.setImgurl("upload\\"+fileName);
} catch (SmartUploadException e) {
e.printStackTrace();
}
BooksDao bd=new BooksDao();
if(bd.add(books)){
response.getWriter().println("add success");
}else {
response.getWriter().println("add error");
}
}
2.3 BooksDao
//添加
public boolean add(Books books){
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = JDBCutils.getConnection();
String sql="insert into books(name,price,catagory,num,description,imgurl) values(?,?,?,?,?,?)";
pst=con.prepareStatement(sql);
pst.setString(1,books.getName());
pst.setDouble(2,books.getPrice());
pst.setString(3,books.getCatagory());
pst.setInt(4,books.getNum());
pst.setString(5,books.getDescription());
pst.setString(6,books.getImgurl());
int i=pst.executeUpdate();
if(i==1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCutils.close(con, pst, rs);
}
return false;
}
三、商品管理
1.删除案例演示
点击删除,删除成功
2.实现步骤
2.1 products/list.jsp
2.2 findProductByIdServlet
//商品删除
if("delete".equals(act)){
String id=request.getParameter("id");
int sid = Integer.parseInt(id);
BooksDao bd=new BooksDao();
if(bd.delete(sid)){
response.getWriter().println("delete success");
}else {
response.getWriter().println("delete error");
}
}
2.3 BooksDao
//删除
public boolean delete(int id){
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = JDBCutils.getConnection();
String sql="delete from books where id=?";
pst=con.prepareStatement(sql);
pst.setInt(1,id);
int i=pst.executeUpdate();
if(i==1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCutils.close(con, pst, rs);
}
return false;
}
四、订单管理
+++.所有订单展示
left.jsp
findProductByIdServlet
//查所有订单
if("all".equals(act)){
OrderDao od=new OrderDao();
List<Order> order=new ArrayList<Order>();
order=od.all();
request.getSession().setAttribute("orders",order);
request.getRequestDispatcher("/admin/orders/list.jsp").forward(request,response);
}
OrdersDao
//所有订单
public List all(){
List<Order> list=new ArrayList<Order>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Users users=new Users();
try {
con = JDBCutils.getConnection();
String sql="select *from orders,users where orders.user_id=users.id";
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()){
Order orders=new Order();
orders.setId(rs.getInt("id"));
orders.setReceiverName(rs.getString("receiverName"));
orders.setReceiverAddress(rs.getString("receiverAddress"));
orders.setReceiverPhone(rs.getString("receiverPhone"));
orders.setMoney(rs.getInt("money"));
orders.setPaystate(rs.getInt("paystate"));
users.setUsername(rs.getString("username"));
orders.setUser(users);
list.add(orders);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
1.查找案例演示
填订单信息查询对应订单
orders/list.jsp
2.实现步骤
2.1 orders/list.jsp
2.2 findProductByIdServlet
//查询订单
if("orders".equals(act)){
String id=request.getParameter("id");
int sid= Integer.parseInt(id);
String receiveName=request.getParameter("receiverName");
Order o=new Order();
o.setId(sid);
o.setReceiverName(receiveName);
OrderDao od=new OrderDao();
List<Order> order=new ArrayList<Order>();
order=od.select(o);
request.getSession().setAttribute("orders",order);
request.getRequestDispatcher("/admin/orders/list.jsp").forward(request,response);
}
2.3 OrdersDao
//查询订单
public List select(Order order){
List<Order> list=new ArrayList<Order>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Order orders=new Order();
Users users=new Users();
try {
con = JDBCutils.getConnection();
String sql="select *from orders,users where orders.user_id=users.id and orders.id=? and receiverName=?";
pst=con.prepareStatement(sql);
pst.setInt(1,order.getId());
pst.setString(2,order.getReceiverName());
rs=pst.executeQuery();
if(rs.next()){
orders.setId(rs.getInt("id"));
orders.setReceiverName(rs.getString("receiverName"));
orders.setReceiverAddress(rs.getString("receiverAddress"));
orders.setReceiverPhone(rs.getString("receiverPhone"));
orders.setMoney(rs.getInt("money"));
orders.setPaystate(rs.getInt("paystate"));
users.setUsername(rs.getString("username"));
orders.setUser(users);
list.add(orders);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
五、订单管理
1.查看案例演示
点击查看进入订单详细信息
view.jsp
2.实现步骤
2.1 orders/list.jsp
2.2 findProductByIdServlet
//显示订单
if("show".equals(act)){
//订单编号order id
String id=request.getParameter("id");
int sid= Integer.parseInt(id);
//orders,users 查询
Order o=new Order();
OrderDao od=new OrderDao();
List<Order> order=new ArrayList<Order>();
order=od.show(sid);
//books,orderItems 查询
List<OrderItem> orderitems=new ArrayList<OrderItem>();
orderitems=od.show2(sid);
request.getSession().setAttribute("orderItems",orderitems);;
request.getSession().setAttribute("order",order);
request.getRequestDispatcher("/admin/orders/view.jsp").forward(request,response);
}
2.3 OrdersDao
//查询订单内容view.jsp中的username和orders
public List show(int id){
List<Order> list=new ArrayList<Order>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Users users=new Users();
try {
con = JDBCutils.getConnection();
String sql="select orders.id,receiverName,receiverAddress,receiverPhone,money,paystate,ordertime,username from orders,users where orders.user_id=users.id and orders.id=?";
pst=con.prepareStatement(sql);
pst.setInt(1,id);
rs=pst.executeQuery();
if(rs.next()){
Order orders=new Order();
orders.setId(rs.getInt("id"));
orders.setReceiverName(rs.getString("receiverName"));
orders.setReceiverAddress(rs.getString("receiverAddress"));
orders.setReceiverPhone(rs.getString("receiverPhone"));
orders.setMoney(rs.getInt("money"));
orders.setPaystate(rs.getInt("paystate"));
orders.setOrdertime(rs.getString("ordertime"));
users.setUsername(rs.getString("username"));
orders.setUser(users);
list.add(orders);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//查询订单内容view.jsp中的buyname和books
public List show2(int id){
List<OrderItem> list=new ArrayList<OrderItem>();
List<Books> list2=new ArrayList<Books>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = JDBCutils.getConnection();
String sql="select * from books,orderItems where books.id=orderItems.book_id and order_id=?";
pst=con.prepareStatement(sql);
pst.setInt(1,id);
rs=pst.executeQuery();
if(rs.next()){
Users users=new Users();
Books p=new Books();
OrderItem oi=new OrderItem();
oi.setBuynum(rs.getInt("buynum"));
p.setId(rs.getInt("book_id"));
p.setName(rs.getString("name"));
p.setPrice(rs.getDouble("price"));
p.setCatagory(rs.getString("catagory"));
p.setDescription(rs.getString("description"));
p.setImgurl(rs.getString("imgurl"));
oi.setP(p);
list.add(oi);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
已支付可以删除
2.1 orders/list.jsp
利用UsersServlet中delete判断即可删除
六、退出系统
1.案例演示
点击退出系统返回到login.jsp
2.实现步骤
2.1 top.jsp
function exitSys() {
var flag = window.confirm("确认退出系统吗?");
if (flag) {
window.top.open('${pageContext.request.contextPath}/findProductByIdServlet?act=exit', '_parent', '');
window.top.close();
}
2.2 findProductByIdServlet
//退出系统
if("exit".equals(act)){
request.getSession().invalidate();
response.sendRedirect(request.getContextPath()+"/client/login.jsp");
}