JSP增删改查.docx_第1页
JSP增删改查.docx_第2页
JSP增删改查.docx_第3页
JSP增删改查.docx_第4页
JSP增删改查.docx_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论