add<电脑列表>
parent
d3cf460f05
commit
b75c396a41
|
@ -0,0 +1,40 @@
|
|||
package example.controller;
|
||||
|
||||
import example.model.Computer;
|
||||
import example.service.IComputerService;
|
||||
import example.service.impl.ComputerServiceImpl;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
@WebServlet(urlPatterns = {"/computerList"})
|
||||
public class ComputerServlet extends HttpServlet {
|
||||
|
||||
IComputerService computerService = new ComputerServiceImpl();
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String path = req.getServletPath();
|
||||
switch (path) {
|
||||
case "/computerList":
|
||||
try {
|
||||
listServlet(req, resp);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void listServlet(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
|
||||
List<Computer> computers= computerService.listComputer();
|
||||
req.setAttribute("computers", computers);
|
||||
req.getRequestDispatcher("computerList.jsp").forward(req, resp);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package example.dao;
|
||||
|
||||
import example.model.Computer;
|
||||
import example.utils.DBUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class ComputerDao {
|
||||
private final QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
|
||||
|
||||
public int addComputer(String name, Double price, int stock) {
|
||||
String sql = "insert into computer(name, price, stock) values(?, ?, ?)";
|
||||
try {
|
||||
return queryRunner.update(sql, name, price, stock);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int updateComputer(String name, Double price, int stock, int id) {
|
||||
String sql = "update computer set name=?, price=?, stock=? where id=?";
|
||||
try {
|
||||
return queryRunner.update(sql, name, price, stock, id);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int deleteComputer(int id) {
|
||||
String sql = "delete from computer where id=?";
|
||||
try {
|
||||
return queryRunner.update(sql, id);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public List<Computer> getAllComputer() throws SQLException {
|
||||
String sql = "select * from computer";
|
||||
return queryRunner.query(sql,new BeanListHandler<>(Computer.class));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package example.model;
|
||||
|
||||
public class Computer {
|
||||
private int id;
|
||||
private String name;
|
||||
private Double price;
|
||||
private int stock;
|
||||
|
||||
public Computer() {
|
||||
}
|
||||
|
||||
public Computer(int id, String name, Double price, int stock) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.stock = stock;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getStock() {
|
||||
return stock;
|
||||
}
|
||||
|
||||
public void setStock(int stock) {
|
||||
this.stock = stock;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package example.service;
|
||||
|
||||
import example.model.Computer;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public interface IComputerService {
|
||||
|
||||
public void addComputer(String name, String brand, String type, String price);
|
||||
|
||||
List<Computer> listComputer() throws SQLException;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package example.service.impl;
|
||||
|
||||
import example.dao.ComputerDao;
|
||||
import example.model.Computer;
|
||||
import example.service.IComputerService;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class ComputerServiceImpl implements IComputerService {
|
||||
|
||||
ComputerDao computerDao=new ComputerDao();
|
||||
|
||||
@Override
|
||||
public void addComputer(String name, String brand, String type, String price) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Computer> listComputer() throws SQLException {
|
||||
return computerDao.getAllComputer();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Computer List</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Computer List</h1>
|
||||
<table border="1">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
<th>Price</th>
|
||||
<th>Stock</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<c:forEach var="computer" items="${computers}">
|
||||
<tr>
|
||||
<td>${computer.id}</td>
|
||||
<td>${computer.name}</td>
|
||||
<td>${computer.price}</td>
|
||||
<td>${computer.stock}</td>
|
||||
<td>
|
||||
<a href="/computer?action=updateForm&id=${computer.id}">Edit</a> |
|
||||
<a href="/computer?action=delete&id=${computer.id}">Delete</a>
|
||||
</td>
|
||||
</tr>
|
||||
</c:forEach>
|
||||
</tbody>
|
||||
</table>
|
||||
<br/>
|
||||
<a href="/computer?action=addForm">Add New Computer</a>
|
||||
</body>
|
||||
</html>
|
|
@ -6,5 +6,6 @@
|
|||
<h2>电脑商城-首页</h2>
|
||||
<a href="/register">注册</a>
|
||||
<a href="/login">登录</a>
|
||||
<a href="/computerList">电脑列表</a>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Reference in New Issue