javaweb商品管理系统实现增删改查代码
时间: 2023-07-11 12:10:09 浏览: 75
以下是一个简单的JavaWeb商品管理系统增删改查的代码示例:
实体类:
```
public class Goods {
private int id;
private String name;
private String type;
private double price;
private int stock;
//省略getter和setter方法
}
```
DAO类:
```
public class GoodsDao {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
//添加商品
public boolean addGoods(Goods goods) {
boolean flag = false;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO goods(name, type, price, stock) VALUES(?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, goods.getName());
ps.setString(2, goods.getType());
ps.setDouble(3, goods.getPrice());
ps.setInt(4, goods.getStock());
int count = ps.executeUpdate();
if(count == 1) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(conn, ps, rs);
}
return flag;
}
//删除商品
public boolean deleteGoods(int id) {
boolean flag = false;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM goods WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
int count = ps.executeUpdate();
if(count == 1) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(conn, ps, rs);
}
return flag;
}
//修改商品
public boolean updateGoods(Goods goods) {
boolean flag = false;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE goods SET name=?, type=?, price=?, stock=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, goods.getName());
ps.setString(2, goods.getType());
ps.setDouble(3, goods.getPrice());
ps.setInt(4, goods.getStock());
ps.setInt(5, goods.getId());
int count = ps.executeUpdate();
if(count == 1) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(conn, ps, rs);
}
return flag;
}
//查询商品
public List<Goods> queryGoods() {
List<Goods> list = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM goods";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setName(rs.getString("name"));
goods.setType(rs.getString("type"));
goods.setPrice(rs.getDouble("price"));
goods.setStock(rs.getInt("stock"));
list.add(goods);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(conn, ps, rs);
}
return list;
}
}
```
Servlet类:
```
public class GoodsServlet extends HttpServlet {
private GoodsDao dao = new GoodsDao();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if("add".equals(action)) {
//添加商品
String name = request.getParameter("name");
String type = request.getParameter("type");
double price = Double.parseDouble(request.getParameter("price"));
int stock = Integer.parseInt(request.getParameter("stock"));
Goods goods = new Goods();
goods.setName(name);
goods.setType(type);
goods.setPrice(price);
goods.setStock(stock);
boolean flag = dao.addGoods(goods);
if(flag) {
response.sendRedirect("goods.jsp");
} else {
response.getWriter().write("添加失败");
}
} else if("delete".equals(action)) {
//删除商品
int id = Integer.parseInt(request.getParameter("id"));
boolean flag = dao.deleteGoods(id);
if(flag) {
response.sendRedirect("goods.jsp");
} else {
response.getWriter().write("删除失败");
}
} else if("update".equals(action)) {
//修改商品
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String type = request.getParameter("type");
double price = Double.parseDouble(request.getParameter("price"));
int stock = Integer.parseInt(request.getParameter("stock"));
Goods goods = new Goods();
goods.setId(id);
goods.setName(name);
goods.setType(type);
goods.setPrice(price);
goods.setStock(stock);
boolean flag = dao.updateGoods(goods);
if(flag) {
response.sendRedirect("goods.jsp");
} else {
response.getWriter().write("修改失败");
}
} else if("query".equals(action)) {
//查询商品
List<Goods> list = dao.queryGoods();
request.setAttribute("list", list);
request.getRequestDispatcher("goods.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
```
以上代码仅为示例,实际开发中需要根据具体需求进行修改和完善。同时,还需要编写JSP页面来呈现商品信息和与用户交互。
阅读全文