第三篇——增加商品
1、补全接口
public interface IProductDao {
List<Product> query();
boolean insert(Product p);
}
2、操作数据库
- 补全代码
public class ProductDaoImpl implements IProductDao {
private PreparedStatement ps;
@Override
public List<Product> query() {
String sql="select * from product";
List<Product> list=new ArrayList();
try {
ps=ConnectionDatabase.getConnection().prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Product p=new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setAddr(rs.getString("addr"));
p.setPrice(rs.getDouble("price"));
list.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public boolean insert(Product p) {
String sql="insert into product(id,name,addr,price) values(?,?,?,?)";
int n=0;
try {
ps=ConnectionDatabase.getConnection().prepareStatement(sql);
ps.setInt(1, p.getId());
ps.setString(2, p.getName());
ps.setString(3, p.getAddr());
ps.setDouble(4, p.getPrice());
n = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n>0;
}
- 测试
public class T {
@Test
public void t() {
IProductDao dao = DaoFactory.getDaoFactory();
Product p=new Product(2,"2","2",2);
System.out.println(dao.insert(p));
}
}
运行结果
查看数据库是否正确添加该条信息
3、业务管理
- 补全增加逻辑管理
public class OperatorServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String op=request.getParameter("op");
if("query".equals(op)) {
queryService(request,response);
}else if("insert".equals(op)){
insertService(request,response);
}
}
private void queryService(HttpServletRequest request, HttpServletResponse response) {
List<Product> list = DaoFactory.getProductDao().query();
try {
if (list != null) {
request.setAttribute("list", list);
request.getRequestDispatcher("product.jsp").forward(request, response);
} else {
response.sendRedirect("main.jsp");
}
} catch (Exception e) {
}
}
private void insertService(HttpServletRequest request, HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String addr = request.getParameter("addr");
double price = Double.parseDouble(request.getParameter("price"));
Product p=new Product(id,name,addr,price);
try {
if (DaoFactory.getProductDao().insert(p)) {
request.getRequestDispatcher("OperatorServlet?op=query").forward(request, response);
} else {
response.sendRedirect("insert.jsp");
}
} catch (Exception e) {
// TODO: handle exception
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4、测试
- 新建insert.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
insert;
</body>
</html>
- 在浏览器搜索栏手动输入url
http://localhost:8081/ProductTest/OperatorServlet?op=insert&&id=6&&name=8&&addr=8&&price=8
5、完善inseert.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
<center>
<form action="OperatorServlet?op=insert" method="post">
<table border="1">
<tr>
<th>编号</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>名称</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>产地</th>
<td><input type="text" name="addr"></td>
</tr>
<tr>
<th>价格</th>
<td><input type="text" name="price"></td>
</tr>
</table>
<input type="submit" value="提交"> <input
type="reset" value="重置">
</form>
</center>
</body>
</html>
运行insert.jsp填写信息并提交查看运行效果
6、完善main.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
<center>
<h2>
<a href="OperatorServlet?op=query" target="show">商品列表</a>
<a href="insert.jsp" target="show">添加商品</a>
</h2>
<iframe name="show" height="800" width="800" frameborder="0"
scrolling="no"></iframe>
</center>
</body>
</html>
运行main.jsp测试运行效果