1,项目结构
2,数据库中建表,创建商品表格goods,项目中建实体Goods,如下:
public class Goods {
private Integer goodsId;
private String goodsName;
private Integer price;
private String brand;
private Integer goodsNum;
//构造方法、get、set方法、toString方法均省略
//.......
}
3, dao接口编写,如图1中,代码如下:
public interface GoodsDao {
//查询所有
public List<Goods> selectAll() throws SQLException;
//添加
public void addGoods(Goods goods) throws SQLException;
//删除
public void delGoods(Integer id) throws SQLException;
//查询一条
public Goods selectOne(Integer id) throws SQLException;
//修改
public void updateGoods(Goods goods) throws SQLException;
}
4,接口实现类
public class GoodsDaoImpl implements GoodsDao {//接口实现类
QueryRunner qr=new QueryRunner(new ComboPooledDataSource());
//查询所有
@Override
public List<Goods> selectAll() throws SQLException {
String sql="select * from goods ";
List<Goods> goodsList = qr.query(sql, new BeanListHandler<Goods>(Goods.class));
return goodsList;
}
//添加
@Override
public void addGoods(Goods goods) throws SQLException {
String sql="INSERT INTO goods(goodsName,price,brand,goodsNum) VALUES(?,?,?,?)";
qr.insert(sql,new BeanHandler<Goods>(Goods.class),goods.getGoodsName(),goods.getPrice(),
goods.getBrand(),goods.getGoodsNum());
}
//删除
@Override
public void delGoods(Integer id) throws SQLException {
String sql="DELETE FROM goods WHERE goodsId = ?";
qr.update(sql,id);
}
//查询一条
@Override
public Goods selectOne(Integer id) throws SQLException {
String sql="SELECT * FROM goods WHERE goodsId=?";
Goods goods = qr.query(sql, new BeanHandler<Goods>(Goods.class), id);
return goods;
}
//修改
@Override
public void updateGoods(Goods goods) throws SQLException {
String sql="UPDATE goods SET goodsName=?,price=?,brand=?,goodsNum=? WHERE goodsId=?";
qr.update(sql,goods.getGoodsName(),goods.getPrice(),goods.getBrand(),
goods.getGoodsNum(),goods.getGoodsId());
}
}
5,查询所有servlet
public class SelectAllServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//字符集
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
GoodsDao goodsDao=new GoodsDaoImpl();
List<Goods> goodsList=null;
try {
goodsList = goodsDao.selectAll();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("goodsList = " + goodsList);
//将结果放到域中
req.setAttribute("list",goodsList);
RequestDispatcher dispatcher = req.getRequestDispatcher( "crudJSP/list.jsp");
dispatcher.forward(req,resp);
}
}
查询所有的展示页面,jsp页面
<body>
<%
List<Goods> list =(List<Goods>) request.getAttribute("list");
%>
<table border="1" width="50%" align="center">
<tr>
<td colspan="7" align="center">商品信息明细表</td>
</tr>
<th>编号</th>
<th>商品名称</th>
<th>价格</th>
<th>品牌</th>
<th>数量</th>
<th>修改</th>
<th>删除</th>
<%
for(Goods goods:list){
%>
<tr>
<td><%=goods.getGoodsId()%></td>
<td><%=goods.getGoodsName()%></td>
<td><%=goods.getPrice()%></td>
<td><%=goods.getBrand()%></td>
<td><%=goods.getGoodsNum()%></td>
<td><a href="<%=request.getContextPath()%>/upEditServlet?id=<%=goods.getGoodsId()%>">修改</a></td>
<td><a href="<%=request.getContextPath()%>/delServlet?id=<%=goods.getGoodsId()%>">删除</a></td>
</tr>
<%
}
%>
<tr><td colspan="7"><a href="crudJSP/add.jsp">添加</a></td></tr>
</table>
</body>
以下的添加功能的servlet
public class AddServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//字符集
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
Integer price = Integer.parseInt(req.getParameter("price"));
String brand = req.getParameter("brand");
Integer num =Integer.parseInt( req.getParameter("num"));
Goods goods=new Goods(name,price,brand,num);
GoodsDao goodsDao=new GoodsDaoImpl();
try {
goodsDao.addGoods(goods);
} catch (SQLException e) {
e.printStackTrace();
}
resp.sendRedirect(req.getContextPath()+"/selectAllServlet");
}
}
以下是添加的编辑页面jsp
<body>
<form action="<%=request.getContextPath()%>/addServlet">
<p>商品名称:<input type="text" name="name" value=""></p>
<p>价格:<input type="text" name="price" value=""></p>
<p>品牌:<input type="text" name="brand" value=""></p>
<p>数量:<input type="text" name="num" value=""></p>
<input type="submit" value="添加">
</form>
</body>
以下是删除的servlet
public class DeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//字符集
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
GoodsDao goodsDao=new GoodsDaoImpl();
/*Integer id = Integer.parseInt(req.getParameter("id"));
goodsDao.delGoods(id);*/
String id = req.getParameter("id");
try {
goodsDao.delGoods(Integer.parseInt(id));
} catch (SQLException e) {
e.printStackTrace();
}
resp.sendRedirect(req.getContextPath()+"/selectAllServlet");
}
}
以下 获取修改的编辑页面实现,servlet
//获取点击的那条记录(数据),然后进入修改编辑页面
public class UpEditServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//字符集
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//从页面获取需要修改某条记录的id
Integer id = Integer.parseInt(req.getParameter("id"));
GoodsDao goodsDao=new GoodsDaoImpl();
Goods goods=null;
try {
goods = goodsDao.selectOne(id);
} catch (SQLException e) {
e.printStackTrace();
}
//将获取一条的记录放入域中
req.setAttribute("goods",goods);
//转发到修改编辑页面
RequestDispatcher dispatcher = req.getRequestDispatcher("crudJSP/updateEdit.jsp");
dispatcher.forward(req,resp);
}
}
以下编辑修改页面jsp
<body>
<%--这个修改编辑页面,获取点击后的那条信息--%>
<%
Goods goods = (Goods)request.getAttribute("goods");
%>
<form action="<%=request.getContextPath()%>/updateSubmitServlet">
<%--id设置为隐藏hidden--%>
<input type="hidden" name="id" value="<%=goods.getGoodsId()%>">
<p>商品名称:<input type="text" name="name" value="<%=goods.getGoodsName()%>"></p>
<p>价格:<input type="text" name="price" value="<%=goods.getPrice()%>"></p>
<p>品牌:<input type="text" name="brand" value="<%=goods.getBrand()%>"></p>
<p>数量:<input type="text" name="num" value="<%=goods.getGoodsNum()%>"></p>
<input type="submit" value="修改">
</form>
</body>
以下提交修改页面,修改的信息存到数据库并展示更新后的所有数据servlet。
//修改编辑页面,点击提交时请求此servlet,对数据库修改,并回到展示全部数据的页面(即请求selectAllServlet)
public class UpSubmitServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//字符集
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//修改页面已经完成,即将把数据保存放到数据库,首先要从页面获取数据
String name = req.getParameter("name");
Integer price = Integer.parseInt(req.getParameter("price")) ;
String brand = req.getParameter("brand");
Integer num =Integer.parseInt(req.getParameter("num")) ;
Integer id = Integer.parseInt(req.getParameter("id"));
//将参数封装起来
Goods goods=new Goods(id,name,price,brand,num);
GoodsDao goodsDao=new GoodsDaoImpl();
try {
goodsDao.updateGoods(goods);
} catch (SQLException e) {
e.printStackTrace();
}
//修改完成后,重定向,进入查询展示页面
resp.sendRedirect(req.getContextPath()+"/selectAllServlet");
}
}