




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
将汉字转为可以识别的光标码 package com; import java.io.UnsupportedEncodingException; public class Change public static String changetoString(String str) String returnstr=null; try byte b=str.getBytes(ISO-8859-1); returnstr=new String(b,GBK); catch (UnsupportedEncodingException e) / TODO Auto-generated catch block e.printStackTrace(); return returnstr; 商品类 package com; public class Product private String product_id; private String product_name; private float product_price; public String getProduct_id() return product_id; public void setProduct_id(String product_id) duct_id = product_id; public String getProduct_name() return product_name; public void setProduct_name(String product_name) duct_name = product_name; public float getProduct_price() return product_price; public void setProduct_price(float product_price) duct_price = product_price; dao处理文件 package com; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class ProductDao public Connection conn() Connection conn=null; try Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver); String URL=jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=chanpin;user=sa;password=sa; conn=DriverManager.getConnection(URL); catch (ClassNotFoundException e) e.printStackTrace(); catch(SQLException e) e.printStackTrace(); return conn; public List getUserList() List list=new ArrayList(); try Connection conn=this.conn(); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(select * from product); while(rs.next() Product pro=new Product(); String product_id=rs.getString(product_id); pro.setProduct_id(product_id); String product_name=rs.getString(product_name); pro.setProduct_name(product_name); float product_price=rs.getFloat(product_price); pro.setProduct_price(product_price); list.add(pro); rs.close(); stmt.close(); this.close(conn); catch (SQLException e) e.printStackTrace(); return list; public Product selectone(String id) Product pro=new Product(); List list=new ArrayList(); try Connection conn=this.conn(); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(select * from product where product_id=+id+); if(rs.next() pro.setProduct_name(rs.getString(2); pro.setProduct_price(rs.getFloat(3); rs.close(); stmt.close(); conn.close(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); return pro; public void delete(String product_id) Connection conn=this.conn(); try Statement stmt=conn.createStatement(); String sql=delete from product where product_id=+product_id+; stmt.executeUpdate(sql); stmt.close(); this.close(conn); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); public void insert(Product pro) String product_name=pro.getProduct_name(); float product_price=pro.getProduct_price(); Connection conn=this.conn(); try Statement stmt=conn.createStatement(); String sql=insert into product(product_name,product_price) values(+product_name+,+product_price+); stmt.executeUpdate(sql); stmt.close(); this.close(conn); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); public void update(Product pro) String product_id=pro.getProduct_id(); String product_name=pro.getProduct_name(); float product_price=pro.getProduct_price(); Connection conn=this.conn(); try Statement stmt=conn.createStatement(); String sql=update product set product_name=+product_name+,product_price=+product_price+ where product_id=+product_id+; stmt.executeUpdate(sql); stmt.close(); this.close(conn); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); public boolean search(String name) boolean i=false; Connection conn=this.conn(); try Statement stmt=conn.createStatement(); String sql=select * from product where product_name=+name+; ResultSet rs=stmt.executeQuery(sql); while(rs.next() i=true; rs.close(); stmt.close(); this.conn(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); return i; public List getnameList(String name) List list=new ArrayList(); try Connection conn=this.conn(); Statement stmt=conn.createStatement(); String sql=select * from product where product_name=+name+; ResultSet rs=stmt.executeQuery(sql); while(rs.next() Product pro=new Product(); String product_id=rs.getString(product_id); pro.setProduct_id(product_id); String product_name=rs.getString(product_name); pro.setProduct_name(product_name); float product_price=rs.getFloat(product_price); pro.setProduct_price(product_price); list.add(pro); rs.close(); stmt.close(); this.close(conn); catch (SQLException e) e.printStackTrace(); return list; public void close(Connection conn) try conn.close(); catch (SQLException e) e.printStackTrace(); servlet文件 package com; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet extends HttpServlet public Servlet() super(); public void destroy() super.destroy(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request,response); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException String kind=request.getParameter(kind); /按条件查询 if(kind.equalsIgnoreCase(select) String product_name=Change.changetoString(request.getParameter(name); ProductDao pdao=new ProductDao(); boolean i=pdao.search(product_name); if(i=false) response.sendRedirect(./fit/notfind.jsp); else request.setAttribute(name123,product_name); RequestDispatcher rd = request.getRequestDispatcher(./fit/selectname.jsp); rd.forward(request,response); /插入信息 if(kind.equalsIgnoreCase(insert) String product_name =Change.changetoString(request.getParameter(name); float product_price = Float.valueOf(request.getParameter(price); List list = new ArrayList(); ProductDao pdao = new ProductDao(); list = pdao.getUserList(); Product pro = null; int i; for (i = 0; i list.size(); i+) pro = (Product) list.get(i); if (product_name.equalsIgnoreCase(pro.getProduct_name() response.sendRedirect(./fit/insert.jsp); break; else if(product_name=null) response.sendRedirect(./fit/insert.jsp); if(i=list.size() Product prod=new Product(); prod.setProduct_name(product_name); prod.setProduct_price(product_price); pdao.insert(prod); response.sendRedirect(./fit/select.jsp); /更新数据 if(kind.equalsIgnoreCase(update) Product pro = new Product(); pro.setProduct_id(request.getParameter(product_id); pro.setProduct_name(Change.changetoString(request.getParameter(product_name); pro.setProduct_price(Float.valueOf(request.getParameter(product_price); ProductDao pdao = new ProductDao(); pdao.update(pro); response.sendRedire
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025下半年四川绵阳安州区考核招聘教师9人备考考试题库附答案解析
- 2025上海宋庆龄幼儿园工作人员招聘1人备考考试题库附答案解析
- 2025下半年陕西咸阳市事业单位招聘209人备考考试题库附答案解析
- 2025广东深圳市退役军人事务局招聘1人备考考试题库附答案解析
- 2025云南玉溪市红塔区发展和改革局城镇公益性岗位招聘1人备考考试题库附答案解析
- 2025版痔疮病情详解及护理方法分享
- 中学组织教育活动实施纲要
- 财税咨询方案写作范文
- 建筑方案设计中标公司名单
- 山东八年级第一学期物理第一次月考9月份考试试题以及答案(适合沪科版)
- 2025至2030中国聚烯烃行业项目调研及市场前景预测评估报告
- 2025四川达州宣汉县国有资产管理服务中心县属国有企业招聘劳动合同职工26人笔试历年参考题库附带答案详解
- 2025年下半年杭州市上城区丁兰街道办事处招聘编外工作人员11人考试参考题库及答案解析
- 2025年合肥市广播电视台(文广集团)招聘12人考试参考题库及答案解析
- 2025年大队委竞选面试题库及答案
- 2025年信用管理专业题库- 信用管理对企业市场风险的控制
- 6.2 用7~9的乘法口诀求商(课件)数学青岛版二年级上册(新教材)
- 普通饮片车间共线生产风险评估报告
- 新教科版小学1-6年级科学需做实验目录
- GB/T 8492-2024一般用途耐热钢及合金铸件
- 读懂诗家语省公开课金奖全国赛课一等奖微课获奖课件
评论
0/150
提交评论