1、Java EE1、package dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class BaseDao private static final String DRIVER=sun.jdbc.odbc.JdbcOdbcDriver; private static final String URL=jdbc:odbc:goods; privat
2、e Connection conn = null; public Connection getConn() try Class.forName(DRIVER); catch (ClassNotFoundException e) / TODO Auto-generated catch block e.printStackTrace(); try conn = DriverManager.getConnection(URL); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); return c
3、onn; public void closeAll(ResultSet rs,Statement stmt,Connection conn) if(rs != null) try rs.close(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); if(stmt != null) try stmt.close(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); if(conn
4、 != null) try conn.close(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); public int update(String sql,String params) int row = 0; conn = this.getConn(); try pstmt = conn.prepareStatement(sql); for(int i =0;iparams.length;i+) pstmt.setString(i+1, paramsi); row = pstmt
5、.executeUpdate(sql); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); finally this.closeAll(null, pstmt, conn); return row; public static void main(String args) BaseDao bd = new BaseDao(); System.out.println(bd.getConn(); 2、package dao;import java.util.List;import entity
6、.Goods;public interface GoodsDao public List findList();/获得商品列表 public int getPageCount(int pagesize);/根据一页显示的记录数,获取结果集中能分的总页数 public Goods getGoods(int id);/根据商品Id获得商品信息 public int delGoods(int id);/根据商品Id删除商品 public int addGoods(Goods goods);/添加商品信息 public int updateGoods(Goods goods);/更新商品信息3、pac
7、kage dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import dao.BaseDao;import dao.GoodsDao;import entity.Goods;public class GoodsDaoImpl extends Ba
8、seDao implements GoodsDao public int delGoods(int id) int row = 0 ; String sql = delete from goods where id = ?; Connection conn = null; PreparedStatement pstmt = null; try conn = super.getConn(); pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); row = pstmt.executeUpdate(); catch (SQLExcepti
9、on e) / TODO Auto-generated catch block e.printStackTrace(); finally super.closeAll(null, pstmt, conn); return row; public List findList() List listGoods = new ArrayList(); String sql = select * from goods order by name asc; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null
10、; try conn = super.getConn(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next() Goods goods = new Goods(); goods.setId(rs.getInt(id); goods.setName(rs.getString(name); goods.setProvider(rs.getString(provider); goods.setPrice(rs.getString(price); goods.setQuantity(rs.getI
11、nt(quantity); listGoods.add(goods); rs.last(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); finally super.closeAll(rs, pstmt, conn); return listGoods; public Goods getGoods(int id) /根据商品Id获得商品信息 String sql = select * from goods where id = ?; Connection conn = null; P
12、reparedStatement pstmt = null; ResultSet rs = null; Goods goods = null; try conn = super.getConn(); pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); rs = pstmt.executeQuery(); if(rs.next() goods = new Goods(); goods.setId(id); goods.setName(rs.getString(name); goods.setProvider(rs.getString(
13、provider); goods.setPrice(rs.getString(price); goods.setQuantity(rs.getInt(quantity); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); finally super.closeAll(rs, pstmt, conn); return goods; public int addGoods(Goods goods) /添加商品信息 int row = 0; String sql = insert into go
14、ods (name,provider,price,quantity) values(?,?,?,?); Connection conn = null; PreparedStatement pstmt = 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,
15、 goods.getQuantity(); row = pstmt.executeUpdate(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); finally super.closeAll(null, pstmt, conn); return row; public int updateGoods(Goods goods) int row = 0; String sql = update goods set name=?,provider=?,price=?,quantity=?
16、where id=?; Connection conn = null; PreparedStatement pstmt = 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, go
17、ods.getId(); row = pstmt.executeUpdate(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); finally super.closeAll(null, pstmt, conn); return row; public static void main(String args) GoodsDao goodsDao = new GoodsDaoImpl();int row = goodsDao.updateGoods(11,aa,ff,1222,121)
18、; if(row0) System.out.println(gengxin成功!); 4、package servelet;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.h
19、ttp.HttpSession;import dao.GoodsDao;import dao.impl.GoodsDaoImpl;import entity.Goods;public class GoodsList extends HttpServlet public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException this.doPost(req, resp);/ super.doGet(req, resp); public void doPost
20、(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException GoodsDao goodsDao = new GoodsDaoImpl(); List list = goodsDao.findList(); HttpSession session = req.getSession(); session.setAttribute(Goodslist, list); resp.sendRedirect(/goods/listServlet.jsp);/ super.doPost(req
21、, resp); 5、listServlet.jsp %taglib prefix=c uri=商品信息列表 商品信息列表 编号 商品名 供货商 售出价格(元) 商品数量(台) 操作 $i& $goods.id&$status.index+1 $goods.name $goods.provider $goods.price $goods.quantity 删除 更新 添加商品 6、package servelet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.
22、http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import dao.GoodsDao;import dao.impl.GoodsDaoImpl;public class Dodelete extends HttpServlet public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOExcept
23、ion int id = Integer.parseInt(req.getParameter(id); GoodsDao goodsDao = new GoodsDaoImpl(); int row = goodsDao.delGoods(id); if(row0) resp.sendRedirect(results.html); / System.out.println(id);/ super.doGet(req, resp); Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub/ super.doPost(req, resp); 7、results.html操作成功操作成功!返回8、更新package servelet;import java.io.IOExc
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1