(转载)Java+MyEclipse+Tomcat (六)详解Servlet和DAO数据库增删改查操作

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
public ProductBean searchinfom(String username){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rst = null; // ProductBean product = new ProductBean(); List<ProductBean> productList=new ArrayList<ProductBean>(); try{ conn = dataSource.getConnection(); pstmt = conn.prepareStatement("SELECT * FROM userproduct WHERE username like ?"); pstmt.setString(1,"%"+username+"%"); rst = pstmt.executeQuery(); while(rst.next()){ ProductBean product=new ProductBean(); product.setUsername(rst.getString("username")); product.setProduct_id(rst.getString("product_id")); product.setFaddress(rst.getString("faddress")); product.setSaddress(rst.getString("saddress")); product.setWeight(rst.getDouble("weight")); product.setPrice(rst.getDouble("price")); productList.add(product); //return product; }//else{ //return null; //} }catch(SQLException se){ System.out.println("SQLException:"+se.getMessage()); return null; }finally{ try{ conn.close(); }catch(SQLException se){ } } return null; } } ``` public class QueryInformServlet extends HttpServlet{ public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ String username = request.getParameter("username"); ProductDao productdao = new ProductDao(); ProductBean product = productdao.searchinfom(username); ArrayList<ProductBean> productList=null; productList =new ArrayList<ProductBean>(); //if(!productList.isEmpty()){ request.getSession().setAttribute("productList",productList); RequestDispatcher view = request.getRequestDispatcher("/displayinform.jsp"); view.forward(request, response); //}else{ // RequestDispatcher view = request.getRequestDispatcher("/error.jsp"); // view.forward(request, response); // } } } ``` <%@ page contentType="text/html;charset=utf-8"%> <%@ page import="java.util.* ,com.bean.ProductBean"%> <html> <body> <table border=1> <tr><td>用户名</td><td>商品号</td><td>发货地</td><td>收货地</td><td>重量</td><td>价格</td></tr> <% ArrayList<ProductBean> productList=(ArrayList<ProductBean>)session.getAttribute("productList"); for(ProductBean product:productList) { %> <tr><td><%= product.getUsername() %></td> <td><%= product.getProduct_id() %></td> <td><%= product.getFaddress() %></td> <td><%= product.getSaddress() %></td> <td><%= product.getWeight() %></td> <td><%= product.getPrice() %></td> </tr> <% } %> </table> </body> </html>
package com.awu.servlet; import java.io.IOException; 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 javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet 获得填写的表单数据 */ public class DeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ request.setCharacterEncoding("UTF-8"); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/mysql" ;//mysql为数据库的名称 String user = "root"; //数据库的登录名 String password = "123456"; //数据库的登录密码 Connection conn=null; conn=DriverManager.getConnection(url, user, password); String flag=request.getParameter("flag"); String users=request.getParameter("id");//用数组接受前台传来的被选中的复选框的值 System.out.printf(users,flag); String[] Id=users.split(","); int m=0; if (Id.length>0) { for (int i = 0; i < Id.length; i++){ int id1=Integer.parseInt(Id[i]); String sql = "delete from test where id="+id1; //test为要查询的表 System.out.println(id1); m++; //根据前台选中的id值循环删除 //将删除落实到数据库删除 ??? //当删除次数等于选中复选框传过来的个数时,删除成功,否则失败,分别返回两种页面 } } if (m==Id.length) { request.setAttribute("delete_infos", "删除成功"); request.getRequestDispatcher("success.jsp").forward(request, response); }else{ request.setAttribute("delete_infos", "删除失败"); request.getRequestDispatcher("login.jsp").forward(request, response); } } catch (SQLException e) { System.out.println(e.getMessage()); } } }
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页