一、MVC存在的意义
二、MVC分层开发简单项目展示
功能:
- 用户登录
- 用户注册
- 商品展示
- 新增商品
- 删除商品
- 更新商品
CommodityController.java
package com.ishopn.controller;
import java.util.List;
import com.ishopn.dao.CommodityDao;
import com.ishopn.dao.impl.CommodityDaoImpl;
import com.ishopn.model.Commodity;
public class CommodityController {
private CommodityDao commodityDao = null;
public CommodityController() {
commodityDao = new CommodityDaoImpl();
}
public List<Commodity> getCommodityList(){
return commodityDao.getCommodityList();
}
public int istCommodity(String c_name, String c_madein, String c_type, String c_inprice, String c_outprice, String c_num) {
Commodity c = new Commodity(System.currentTimeMillis()+"", c_name, c_madein, new Integer(c_type), new Integer(c_inprice), new Integer(c_outprice), new Integer(c_num));
return commodityDao.istCommodity(c);
}
public int delCommodityById(String c_id) {
return commodityDao.delCommodityById(c_id);
}
public Commodity getCommodityById(String c_id) {
return commodityDao.getCommodityById(c_id);
}
public int updCommodity(String c_id, String c_name, String c_madein, String c_type, String c_inprice, String c_outprice, String c_num) {
Commodity c = new Commodity(c_id, c_name, c_madein, new Integer(c_type), new Integer(c_inprice), new Integer(c_outprice), new Integer(c_num));
return commodityDao.updCommodity(c);
}
}
CommoditytypeController.java
package com.ishopn.controller;
import java.util.List;
import com.ishopn.dao.CommoditytypeDao;
import com.ishopn.dao.impl.CommoditytypeDaoImpl;
import com.ishopn.model.Commoditytype;
public class CommoditytypeController {
private CommoditytypeDao commoditytypeDao = null;
public CommoditytypeController() {
commoditytypeDao = new CommoditytypeDaoImpl();
}
public List<Commoditytype> getCommoditytypeList(){
return commoditytypeDao.getCommoditytypeList();
}
}
CustomerController.java
package com.ishopn.controller;
import java.util.UUID;
import com.ishopn.dao.CustomerDao;
import com.ishopn.dao.impl.CustomerDaoImpl;
import com.ishopn.model.Customer;
/**
* Cotroller层:业务逻辑处理
* @author Sowhite
*
*/
public class CustomerController {
private CustomerDao customerDao;
public CustomerController() {
customerDao = new CustomerDaoImpl();
}
/**
* 面向对象的思想,我们后面所有层和层之间的数据的传递,不再使用基本数据类型,都使用对象来传递
* @param cu_name
* @param cu_phone
* @return
*/
public int getCountCustomerByNameAndPhone(String cu_name, String cu_phone) {
Customer cu = new Customer();
cu.setCu_name(cu_name);
cu.setCu_phone(cu_phone);
return customerDao.getCountCustomerByNameAndPhone(cu);
}
/**
* 新增用户
* @param cu_name
* @param cu_phone
* @param cu_gender
* @param cu_address
* @return
*/
public int istCustomer(String cu_name, String cu_phone, String cu_gender, String cu_address) {
Customer cu = new Customer(UUID.randomUUID().toString(), cu_name, cu_phone, new Integer(cu_gender), cu_address);
return customerDao.istCustomer(cu);
}
}
CommodityDao.java
package com.ishopn.dao;
import java.util.List;
import com.ishopn.model.Commodity;
public interface CommodityDao {
List<Commodity> getCommodityList();
int istCommodity(Commodity c);
int delCommodityById(String c_id);
Commodity getCommodityById(String c_id);
int updCommodity(Commodity c);
}
CommoditytypeDao.java
package com.ishopn.dao;
import java.util.List;
import com.ishopn.model.Commoditytype;
public interface CommoditytypeDao {
Commoditytype getCommoditytypeById(Integer ct_id);
List<Commoditytype> getCommoditytypeList();
}
CustomerDao.java
package com.ishopn.dao;
import com.ishopn.model.Customer;
/**
* Data Access Object 数据(库)访问层
*/
/**
* 对哪张表的数据库操作,就书写在对应的Dao中
* 本Dao对Customer表操作:登录和注册(作业)
* 我们约定,数据库访问的时候方法的名称
* select > get...
* insert > ist...
* delete > del...
* update > upd
* @author Sowhite
*
*/
public interface CustomerDao {
int getCountCustomerByNameAndPhone(Customer cu);
int istCustomer(Customer cu);
}
CommodityDaoImpl.java
package com.ishopn.dao.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.ishopn.dao.CommodityDao;
import com.ishopn.model.Commodity;
import com.ishopn.util.DBHelper;
public class CommodityDaoImpl implements CommodityDao{
@Test
public List<Commodity> getCommodityList() {
List<Commodity> commodities = new ArrayList<Commodity>();
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select * from Commodity";
ResultSet rs = DBHelper.executeQuery(conn, sql, null);
while(rs.next()) {
Commodity c = new Commodity();
c.setC_id(rs.getString("c_id"));
c.setC_name(rs.getString("c_name"));
c.setC_madein(rs.getString("c_madein"));
c.setC_type(rs.getInt("c_type"));
c.setC_inprice(rs.getInt("c_inprice"));
c.setC_outprice(rs.getInt("c_outprice"));
c.setC_num(rs.getInt("c_num"));
c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getInt("c_type")));
commodities.add(c);
}
DBHelper.closeConnection(conn);
}catch(Exception e) {
e.printStackTrace();
}
return commodities;
}
@Override
public int istCommodity(Commodity c) {
int line = 0;
Connection conn = DBHelper.getConnection();
String sql = "insert into commodity (c_id,c_name,c_madein,c_type,c_inprice,c_outprice,c_num) values (?,?,?,?,?,?,?)";
List param = new ArrayList();
param.add(c.getC_id());
param.add(c.getC_name());
param.add(c.getC_madein());
param.add(c.getC_type());
param.add(c.getC_inprice());
param.add(c.getC_outprice());
param.add(c.getC_num());
line = DBHelper.executeUpdate(conn, sql, param);
DBHelper.closeConnection(conn);
return line;
}
@Override
public int delCommodityById(String c_id) {
int line = 0;
Connection conn = DBHelper.getConnection();
String sql = "delete from commodity where c_id=?";
List param = new ArrayList();
param.add(c_id);
line = DBHelper.executeUpdate(conn, sql, param);
DBHelper.closeConnection(conn);
return line;
}
@Override
public Commodity getCommodityById(String c_id) {
Commodity c = new Commodity();
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select * from Commodity where c_id=?";
List param = new ArrayList();
param.add(c_id);
ResultSet rs = DBHelper.executeQuery(conn, sql, param);
rs.next();
c.setC_id(rs.getString("c_id"));
c.setC_name(rs.getString("c_name"));
c.setC_madein(rs.getString("c_madein"));
c.setC_type(rs.getInt("c_type"));
c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getInt("c_type")));
c.setC_inprice(rs.getInt("c_inprice"));
c.setC_outprice(rs.getInt("c_outprice"));
c.setC_num(rs.getInt("c_num"));
DBHelper.closeConnection(conn);
}catch(Exception e) {
}
return c;
}
@Override
public int updCommodity(Commodity c) {
int line = 0;
Connection conn = DBHelper.getConnection();
String sql = "update commodity set c_name=?,c_madein=?,c_type=?,c_inprice=?,c_outprice=?,c_num=? where c_id=?";
List param = new ArrayList();
param.add(c.getC_name());
param.add(c.getC_madein());
param.add(c.getC_type());
param.add(c.getC_inprice());
param.add(c.getC_outprice());
param.add(c.getC_num());
param.add(c.getC_id());
line = DBHelper.executeUpdate(conn, sql, param);
DBHelper.closeConnection(conn);
return line;
}
}
CommoditytypeDaoImpl.java
package com.ishopn.dao.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.ishopn.dao.CommoditytypeDao;
import com.ishopn.model.Commoditytype;
import com.ishopn.util.DBHelper;
public class CommoditytypeDaoImpl implements CommoditytypeDao{
@Test
public Commoditytype getCommoditytypeById(Integer ct_id) {
Commoditytype ct = null;
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select * from commoditytype where ct_id=?";
List<Object> param = new ArrayList<Object>();
param.add(ct_id);
ResultSet rs = DBHelper.executeQuery(conn, sql, param);
rs.next();
ct = new Commoditytype(rs.getInt("ct_id"), rs.getString("ct_name"));
DBHelper.closeConnection(conn);
}catch(Exception e) {
e.printStackTrace();
}
return ct;
}
@Override
public List<Commoditytype> getCommoditytypeList() {
List<Commoditytype> cs = new ArrayList<Commoditytype>();
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select * from Commoditytype";
ResultSet rs = DBHelper.executeQuery(conn, sql, null);
while(rs.next()) {
Commoditytype ct = new Commoditytype();
ct.setCt_id(rs.getInt("ct_id"));
ct.setCt_name(rs.getString("ct_name"));
cs.add(ct);
}
DBHelper.closeConnection(conn);
}catch(Exception e) {
e.printStackTrace();
}
return cs;
}
}
CustomerDaoImpl.java
package com.ishopn.dao.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.ishopn.dao.CustomerDao;
import com.ishopn.model.Customer;
import com.ishopn.util.DBHelper;
/**
* DAO实现类(对DAO接口的实现)
* * @author Sowhite
*
*/
public class CustomerDaoImpl implements CustomerDao {
@Test
public int getCountCustomerByNameAndPhone(Customer cu) {
int count = 0;
try {
// 1、获取数据库连接
Connection conn = DBHelper.getConnection();
// 3、编写sql命令
String sql = "select count(*) from customer where cu_name=? and cu_phone=?";
List param = new ArrayList();
param.add(cu.getCu_name());
param.add(cu.getCu_phone());
// 4、执行命令,返回资源结果集
ResultSet rs = DBHelper.executeQuery(conn, sql, param);
rs.next();
count = rs.getInt(1);
// 2、关闭数据库连接
DBHelper.closeConnection(conn);
}catch(Exception e) {
e.printStackTrace();
}
return count;
}
@Override
public int istCustomer(Customer cu) {
int line = 0; // 返回数据库受影响行数
Connection conn = DBHelper.getConnection();
String sql = "insert into customer (cu_id,cu_name,cu_phone,cu_gender,cu_address) values (?,?,?,?,?)";
List param = new ArrayList();
param.add(cu.getCu_id()); // cu_id 既可以在这里使用uuid,也可以在control那里new uuid
param.add(cu.getCu_name());
param.add(cu.getCu_phone());
param.add(cu.getCu_gender());
param.add(cu.getCu_address());
line = DBHelper.executeUpdate(conn, sql, param);
DBHelper.closeConnection(conn);
return line;
}
}
Commodity.java
package com.ishopn.model;
/**
* 商品表
* @author Sowhite
*
*/
public class Commodity {
private String c_id;
private String c_name;
private String c_madein;
private Integer c_type; // c_type能够确定位移的ct 1
private Commoditytype ct; // ct(ct_id:1, ct_name:'玩具')
private Integer c_inprice;
private Integer c_outprice;
private Integer c_num;
public Commodity() {
super();
}
public Commodity(String c_id, String c_name, String c_madein, Integer c_type, Commoditytype ct, Integer c_inprice,
Integer c_outprice, Integer c_num) {
super();
this.c_id = c_id;
this.c_name = c_name;
this.c_madein = c_madein;
this.c_type = c_type;
this.ct = ct;
this.c_inprice = c_inprice;
this.c_outprice = c_outprice;
this.c_num = c_num;
}
public Commodity(String c_id, String c_name, String c_madein, Integer c_type, Integer c_inprice,
Integer c_outprice, Integer c_num) {
super();
this.c_id = c_id;
this.c_name = c_name;
this.c_madein = c_madein;
this.c_type = c_type;
this.c_inprice = c_inprice;
this.c_outprice = c_outprice;
this.c_num = c_num;
}
public String getC_id() {
return c_id;
}
public void setC_id(String c_id) {
this.c_id = c_id;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public String getC_madein() {
return c_madein;
}
public void setC_madein(String c_madein) {
this.c_madein = c_madein;
}
public Integer getC_type() {
return c_type;
}
public void setC_type(Integer c_type) {
this.c_type = c_type;
}
public Commoditytype getCt() {
return ct;
}
public void setCt(Commoditytype ct) {
this.ct = ct;
}
public Integer getC_inprice() {
return c_inprice;
}
public void setC_inprice(Integer c_inprice) {
this.c_inprice = c_inprice;
}
public Integer getC_outprice() {
return c_outprice;
}
public void setC_outprice(Integer c_outprice) {
this.c_outprice = c_outprice;
}
public Integer getC_num() {
return c_num;
}
public void setC_num(Integer c_num) {
this.c_num = c_num;
}
@Override
public String toString() {
return "Commodity [c_id=" + c_id + ", c_name=" + c_name + ", c_madein=" + c_madein + ", c_type=" + c_type
+ ", ct=" + ct + ", c_inprice=" + c_inprice + ", c_outprice=" + c_outprice + ", c_num=" + c_num + "]";
}
}
Commoditytype.java
package com.ishopn.model;
import java.util.List;
/**
* 商品类型表
* @author Sowhite
*
*/
public class Commoditytype {
private Integer ct_id; // 1
// private List<Commodity> commoditys; // 和在Commodity类里写private Commoditytype ct有相同效果,但两者只能二选一写
private String ct_name;
public Commoditytype() {
super();
}
public Commoditytype(Integer ct_id, String ct_name) {
super();
this.ct_id = ct_id;
this.ct_name = ct_name;
}
public Integer getCt_id() {
return ct_id;
}
public void setCt_id(Integer ct_id) {
this.ct_id = ct_id;
}
public String getCt_name() {
return ct_name;
}
public void setCt_name(String ct_name) {
this.ct_name = ct_name;
}
@Override
public String toString() {
return "Commoditytype [ct_id=" + ct_id + ", ct_name=" + ct_name + "]";
}
}
Customer.java
package com.ishopn.model;
/**
* Model层:将数据库中的数据表映射成Java类
*/
/**
* 对应客户表的实体类
* @author Sowhite
*
*/
public class Customer {
private String cu_id;
private String cu_name;
private String cu_phone;
private Integer cu_gender;
private String cu_address;
public Customer() {
super();
}
public Customer(String cu_id, String cu_name, String cu_phone, Integer cu_gender, String cu_address) {
super();
this.cu_id = cu_id;
this.cu_name = cu_name;
this.cu_phone = cu_phone;
this.cu_gender = cu_gender;
this.cu_address = cu_address;
}
public String getCu_id() {
return cu_id;
}
public void setCu_id(String cu_id) {
this.cu_id = cu_id;
}
public String getCu_name() {
return cu_name;
}
public void setCu_name(String cu_name) {
this.cu_name = cu_name;
}
public String getCu_phone() {
return cu_phone;
}
public void setCu_phone(String cu_phone) {
this.cu_phone = cu_phone;
}
public Integer getCu_gender() {
return cu_gender;
}
public void setCu_gender(Integer cu_gender) {
this.cu_gender = cu_gender;
}
public String getCu_address() {
return cu_address;
}
public void setCu_address(String cu_address) {
this.cu_address = cu_address;
}
@Override
public String toString() {
return "Customer [cu_id=" + cu_id + ", cu_name=" + cu_name + ", cu_phone=" + cu_phone + ", cu_gender="
+ cu_gender + ", cu_address=" + cu_address + "]";
}
}
LoginServlet.java
package com.ishopn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CommodityController;
import com.ishopn.controller.CommoditytypeController;
import com.ishopn.controller.CustomerController;
import com.ishopn.model.Commodity;
import com.ishopn.model.Commoditytype;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
// 从前台页面获取参数
String cu_name = req.getParameter("cu_name");
String cu_phone = req.getParameter("cu_phone");
CustomerController cc = new CustomerController();
int count = cc.getCountCustomerByNameAndPhone(cu_name, cu_phone);
if(count > 0) {
Cookie cookie = new Cookie("cu_name", cu_name);
cookie.setMaxAge(60*60*24*7);
resp.addCookie(cookie);
HttpSession session = req.getSession();
session.setAttribute("cu_name", cu_name);
List<Commodity> cList = new CommodityController().getCommodityList();
session.setAttribute("cList", cList);
List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
session.setAttribute("ctList", ctList);
resp.sendRedirect("/day_0906_web6/welcome.jsp");
}else {
resp.sendRedirect("/day_0906_web6");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
InsertCustomerServlet.java
package com.ishopn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CommodityController;
import com.ishopn.controller.CommoditytypeController;
import com.ishopn.controller.CustomerController;
import com.ishopn.model.Commodity;
import com.ishopn.model.Commoditytype;
public class InsertCustomerServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String cu_name = req.getParameter("cu_name");
String cu_phone = req.getParameter("cu_phone");
String cu_gender = req.getParameter("cu_gender");
String cu_address = req.getParameter("cu_address");
CustomerController cc = new CustomerController();
int line = cc.istCustomer(cu_name, cu_phone, cu_gender, cu_address);
if(line>0) {
Cookie cookie = new Cookie("cu_name", cu_name);
cookie.setMaxAge(60*60*24*7);
resp.addCookie(cookie);
HttpSession session = req.getSession();
session.setAttribute("cu_name", cu_name);
List<Commodity> cList = new CommodityController().getCommodityList();
session.setAttribute("cList", cList);
List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
session.setAttribute("ctList", ctList);
resp.sendRedirect("/day_0906_web6/welcome.jsp");
}else {
resp.sendRedirect("/day_0906_web6"); // 返回主页err=2注册失败,请与管理员联系
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
InsertCommodityServlet.java
package com.ishopn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CommodityController;
import com.ishopn.controller.CommoditytypeController;
import com.ishopn.model.Commodity;
import com.ishopn.model.Commoditytype;
public class InsertCommodityServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String c_name = req.getParameter("c_name");
String c_madein = req.getParameter("c_madein");
String c_type = req.getParameter("c_type");
String c_inprice = req.getParameter("c_inprice");
String c_outprice = req.getParameter("c_outprice");
String c_num = req.getParameter("c_num");
CommodityController cc = new CommodityController();
int line = cc.istCommodity(c_name, c_madein, c_type, c_inprice, c_outprice, c_num);
if(line > 0) {
HttpSession session = req.getSession();
List<Commodity> cList = new CommodityController().getCommodityList();
session.setAttribute("cList", cList);
List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
session.setAttribute("ctList", ctList);
resp.sendRedirect("/day_0906_web6/welcome.jsp");
}else {
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
DeleteCommodityById.java
package com.ishopn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CommodityController;
import com.ishopn.controller.CommoditytypeController;
import com.ishopn.model.Commodity;
import com.ishopn.model.Commoditytype;
public class DeleteCommodityById extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String c_id = req.getParameter("c_id");
CommodityController cc = new CommodityController();
int line = cc.delCommodityById(c_id);
if(line > 0) {
HttpSession session = req.getSession();
List<Commodity> cList = new CommodityController().getCommodityList();
session.setAttribute("cList", cList);
List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
session.setAttribute("ctList", ctList);
resp.sendRedirect("/day_0906_web6/welcome.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
ToUpdateServlet.java
package com.ishopn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CommodityController;
import com.ishopn.controller.CommoditytypeController;
import com.ishopn.model.Commodity;
import com.ishopn.model.Commoditytype;
public class ToUpdateServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String c_id = req.getParameter("c_id");
CommodityController cc = new CommodityController();
Commodity c = cc.getCommodityById(c_id);
List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
if(c != null) {
HttpSession session = req.getSession();
session.setAttribute("commodity", c);
session.setAttribute("ctList", ctList);
resp.sendRedirect("/day_0906_web6/commodity.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
DoUpdateServlet.java
package com.ishopn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CommodityController;
import com.ishopn.controller.CommoditytypeController;
import com.ishopn.model.Commodity;
import com.ishopn.model.Commoditytype;
public class DoUpdateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String c_id = req.getParameter("c_id");
String c_name = req.getParameter("c_name");
String c_madein = req.getParameter("c_madein");
String c_type = req.getParameter("c_type");
String c_inprice = req.getParameter("c_inprice");
String c_outprice = req.getParameter("c_outprice");
String c_num = req.getParameter("c_num");
CommodityController cc = new CommodityController();
int line = cc.updCommodity(c_id, c_name, c_madein, c_type, c_inprice, c_outprice, c_num);
if(line > 0) {
HttpSession session = req.getSession();
List<Commodity> cList = new CommodityController().getCommodityList();
session.setAttribute("cList", cList);
List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
session.setAttribute("ctList", ctList);
resp.sendRedirect("/day_0906_web6/welcome.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
DBhelper.java
package com.ishopn.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* 这个类是我们的数据库访问的工具类
* 将数据库连接使用得公用部分抽离出来
* 获取数据库连接
* 关闭数据库连接
* 统一的查询方法
* 统一的更新方法
* @author Sowhite
*
*/
public class DBHelper {
private static String url = "jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";
private static String user = "xxx";
private static String password = "xxx";
/**
* 获取数据库连接对象
* @return 数据库连接对象
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 统一关闭数据库连接对象
* @param conn 数据库连接实例对象
*/
public static void closeConnection(Connection conn) {
if(conn != null) {
try {
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* 通用的查询方法
* @param conn 数据库连接对象
* @param sql 需要执行的sql语句
* @param param sql语句中需要替换的参数列表
* @return ResultSet资源结果集
*/
public static ResultSet executeQuery(Connection conn, String sql, List param) {
ResultSet rs = null;
try {
PreparedStatement ps = conn.prepareStatement(sql);
if(param != null && param.size()>0) {
for(int i=0; i<param.size(); i++) {
// setObject(i+1,arg[i])用法与setInt(i+1,arg[i]),setString(i+1,arg[i])用法类似
// 其中,第一个是指SQL语句中的第几个参数,第二个是要设置的值
ps.setObject(i+1, param.get(i));
}
}
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
/**
* 通用的更新方法
* @param conn 数据库连接对象
* @param sql 需要执行的sql语句
* @param param sql语句中需要替换的参数列表
* @return line 受影响行数
*/
public static int executeUpdate(Connection conn, String sql, List param) {
int line = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql);
if(param != null && param.size()>0) {
for(int i=0; i<param.size(); i++) {
// setObject(i+1,arg[i])用法与setInt(i+1,arg[i]),setString(i+1,arg[i])用法类似
// 其中,第一个是指SQL语句中的第几个参数,第二个是要设置的值
ps.setObject(i+1, param.get(i));
}
}
line = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>day_0906_web6</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.ishopn.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>insertCustomerServlet</servlet-name>
<servlet-class>com.ishopn.servlet.InsertCustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>insertCustomerServlet</servlet-name>
<url-pattern>/insertCustomerServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>insertCommodityServlet</servlet-name>
<servlet-class>com.ishopn.servlet.InsertCommodityServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>insertCommodityServlet</servlet-name>
<url-pattern>/insertCommodityServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deleteCommodityById</servlet-name>
<servlet-class>com.ishopn.servlet.DeleteCommodityById</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>deleteCommodityById</servlet-name>
<url-pattern>/deleteCommodityById</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>toUpdateServlet</servlet-name>
<servlet-class>com.ishopn.servlet.ToUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>toUpdateServlet</servlet-name>
<url-pattern>/toUpdateServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>doUpdateServlet</servlet-name>
<servlet-class>com.ishopn.servlet.DoUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>doUpdateServlet</servlet-name>
<url-pattern>/doUpdateServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<%
String cu_name = "";
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length >0){
for(Cookie c : cookies){
if("cu_name".equals(c.getName())){
cu_name = c.getValue();
}
}
}
%>
</head>
<body>
<form action="/day_0906_web6/loginServlet" method="post">
用户名称:<input type="text" value="<%=cu_name %>" name="cu_name" /><br />
用户手机:<input type="text" name="cu_phone" /><br />
<input type="submit" value="登录" /><a href="/day_0906_web6/regist.jsp">还没有注册?</a>
</form>
</body>
</html>
regist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/day_0906_web6/insertCustomerServlet" method="post">
用户名称:<input type="text" name="cu_name" /><br />
用户电话:<input type="text" name="cu_phone" /><br />
用户性别:
<select name="cu_gender">
<option value="1" selected>男</option>
<option value="0">女</option>
</select><br />
用户地址:<input type="text" name="cu_address" /><br />
<input type="submit" value="注册" /><a href="/day_0906_web6">已经注册了</a>
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.ishopn.model.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎界面</title>
<%
List<Commoditytype> ctList = (ArrayList<Commoditytype>)session.getAttribute("ctList");
List<Commodity> cList = (ArrayList<Commodity>)session.getAttribute("cList");
%>
</head>
<body>
<h1>欢迎您!<%=session.getAttribute("cu_name") %></h1>
<hr />
<!-- 商品列表的展示 List<Commodity> -->
<form method="post" action="/day_0906_web6/insertCommodityServlet">
<table>
<tr>
<td colspan="1"><input type="text" name="c_name" /></td>
<td><input type="text" name="c_madein" /></td>
<td>
<select name="c_type">
<%
for(Commoditytype ct : ctList)
{
%>
<option value="<%=ct.getCt_id() %>"><%=ct.getCt_name() %></option>
<%
}
%>
</select>
</td>
<td><input type="text" name="c_inprice" /></td>
<td><input type="text" name="c_outprice" /></td>
<td><input type="text" name="c_num" /></td>
<td><input type="submit" name="新增" /></td>
</tr>
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品产地</td>
<td>商品类型</td>
<td>商品进价</td>
<td>商品售价</td>
<td>商品库存</td>
<td>编辑|删除</td>
</tr>
<%
for(Commodity c : cList){
%>
<tr>
<td><%=c.getC_id() %></td>
<td><%=c.getC_name() %></td>
<td><%=c.getC_madein() %></td>
<td><%=c.getCt().getCt_name() %></td>
<td><%=c.getC_inprice() %></td>
<td><%=c.getC_outprice() %></td>
<td><%=c.getC_num() %></td>
<td><a href="/day_0906_web6/toUpdateServlet?c_id=<%=c.getC_id() %>">编辑</a>|<a href="/day_0906_web6/deleteCommodityById?c_id=<%=c.getC_id() %>">删除</a></td>
</tr>
<%
}
%>
</table>
</form>
</body>
</html>
commodity.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.ishopn.model.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品信息修改页面</title>
<%
Commodity c = (Commodity)session.getAttribute("commodity");
List<Commoditytype> ctList = (ArrayList<Commoditytype>)session.getAttribute("ctList");
%>
</head>
<body>
<form action="/day_0906_web6/doUpdateServlet" method="post">
商品编号:<input type="text" name="c_id" value="<%=c.getC_id() %>" readonly /><br />
商品名称:<input type="text" name="c_name" value="<%=c.getC_name() %>"><br />
商品产地:<input type="text" name="c_madein" value="<%=c.getC_madein() %>"><br />
商品种类:
<select name="c_type">
<%
for(Commoditytype ct : ctList){
%>
<option value="<%=ct.getCt_id() %>" <%=ct.getCt_id()==c.getC_type()?"selected":"" %>><%=ct.getCt_name() %></option>
<%
}
%>
</select><br />
商品进价:<input type="text" name="c_inprice" value="<%=c.getC_inprice() %>"><br />
商品售价:<input type="text" name="c_outprice" value="<%=c.getC_outprice() %>"><br />
商品库存:<input type="text" name="c_num" value="<%=c.getC_num() %>"><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
效果:
index.jsp
regist.jsp
welcome.jsp
commodity.jsp
推荐一篇更详细的博客:https://blog.csdn.net/u010168160/article/details/43150049