pstmt.setString(i+1,params[i]);
}
row=pstmt.executeUpdate(sql);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
this.closeAll(null,pstmt,conn);
}
returnrow;
}
publicstaticvoidmain(String[]args){
BaseDaobd=newBaseDao();
System.out.println(bd.getConn());
}
}
2、
packagedao;
importjava.util.List;
importentity.Goods;
publicinterfaceGoodsDao{
publicListfindList();//获得商品列表
publicintgetPageCount(intpagesize);//根据一页显示的记录数,获取结果集中能分的总页数
publicGoodsgetGoods(intid);//根据商品Id获得商品信息
publicintdelGoods(intid);//根据商品Id删除商品
publicintaddGoods(Goodsgoods);//添加商品信息
publicintupdateGoods(Goodsgoods);//更新商品信息
}
3、
packagedao.impl;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;
importjava.util.List;
importdao.BaseDao;
importdao.GoodsDao;
importentity.Goods;
publicclassGoodsDaoImplextendsBaseDaoimplementsGoodsDao{
publicintdelGoods(intid){
introw=0;
Stringsql="deletefromgoodswhereid=?
";
Connectionconn=null;
PreparedStatementpstmt=null;
try{
conn=super.getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
row=pstmt.executeUpdate();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
super.closeAll(null,pstmt,conn);
}
returnrow;
}
publicListfindList(){
ListlistGoods=newArrayList();
Stringsql="select*fromgoodsorderbynameasc";
Connectionconn=null;
PreparedStatementpstmt=null;
ResultSetrs=null;
try{
conn=super.getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
Goodsgoods=newGoods();
goods.setId(rs.getInt("id"));
goods.setName(rs.getString("name"));
goods.setProvider(rs.getString("provider"));
goods.setPrice(rs.getString("price"));
goods.setQuantity(rs.getInt("quantity"));
listGoods.add(goods);
}
rs.last();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
super.closeAll(rs,pstmt,conn);
}
returnlistGoods;
}
publicGoodsgetGoods(intid){
//根据商品Id获得商品信息
Stringsql="select*fromgoodswhereid=?
";
Connectionconn=null;
PreparedStatementpstmt=null;
ResultSetrs=null;
Goodsgoods=null;
try{
conn=super.getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
if(rs.next()){
goods=newGoods();
goods.setId(id);
goods.setName(rs.getString("name"));
goods.setProvider(rs.getString("provider"));
goods.setPrice(rs.getString("price"));
goods.setQuantity(rs.getInt("quantity"));
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
super.closeAll(rs,pstmt,conn);
}
returngoods;
}
publicintaddGoods(Goodsgoods){
//添加商品信息
introw=0;
Stringsql="insertintogoods(name,provider,price,quantity)values(?
?
?
?
)";
Connectionconn=null;
PreparedStatementpstmt=null;
try{
conn=super.getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,goods.getName());
pstmt.setString(2,goods.getProvider());
pstmt.setString(3,goods.getPrice());
pstmt.setInt(4,goods.getQuantity());
row=pstmt.executeUpdate();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
super.closeAll(null,pstmt,conn);
}
returnrow;
}
publicintupdateGoods(Goodsgoods){
introw=0;
Stringsql="updategoodssetname=?
provider=?
price=?
quantity=?
whereid=?
";
Connectionconn=null;
PreparedStatementpstmt=null;
try{
conn=super.getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,goods.getName());
pstmt.setString(2,goods.getProvider());
pstmt.setString(3,goods.getPrice());
pstmt.setInt(4,goods.getQuantity());
pstmt.setInt(5,goods.getId());
row=pstmt.executeUpdate();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
super.closeAll(null,pstmt,conn);
}
returnrow;
}
publicstaticvoidmain(String[]args){
GoodsDaogoodsDao=newGoodsDaoImpl();
introw=goodsDao.updateGoods(11,"aa","ff","1222",121);
if(row>0){
System.out.println("gengxin成功!
");
}
}
4、
packageservelet;
importjava.io.IOException;
importjava.util.List;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importdao.GoodsDao;
importdao.impl.GoodsDaoImpl;
importentity.Goods;
publicclassGoodsListextendsHttpServlet{
publicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
this.doPost(req,resp);
//super.doGet(req,resp);
}
publicvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
GoodsDaogoodsDao=newGoodsDaoImpl();
Listlist=goodsDao.findList();
HttpSessionsession=req.getSession();
session.setAttribute("Goodslist",list);
resp.sendRedirect("/goods/listServlet.jsp");
//super.doPost(req,resp);
}
}
5、listServlet.jsp
<%@pagelanguage="java"contentType="text/html;charset=utf-8"
pageEncoding="utf-8"%>
<%@taglibprefix="c"uri="
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:
//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@pageimport="dao.impl.*,dao.*,java.util.*"%>
<%@pageimport="entity.Goods"%>//www.w3.org/1999/xhtml">
商品信息列表