package com.demo; import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.WebServlet; @WebServlet("/queryproduct.do") public class QueryProductServlet extends HttpServlet{ private static final long serialVersionUID = 1L; Connection dbconn = null; public void init() { String driver = "com.jdbc.MysqL.Driver"; String dburl = "jdbc:MysqL://localhost:3306/prod_data"; String username = "root"; String password = "123456"; try{ Class.forName(driver); // 加载驱动程序 // 创建连接对象 dbconn = DriverManager.getConnection(dburl,username,password); }catch(ClassNotFoundException e1){ System.out.println(e1); }catch(sqlException e2){} } public void doPost(HttpServletRequest request, HttpServletResponse response) throws servletexception,IOException{ String productid = request.getParameter("productid"); try{ String sql="SELECT * FROM products WHERE prod_id = ?"; PreparedStatement pstmt = dbconn.prepareStatement(sql); pstmt.setString(1,productid); ResultSet rst = pstmt.executeQuery(); //System.out.println(sql); if(rst.next()){ Product product = new Product(); product.setProd_id(rst.getString("prod_id")); product.setPname(rst.getString("pname")); product.setPrice(rst.getDouble("price")); product.setStock(rst.getInt("stock")); request.getSession().setAttribute("product", product); response.sendRedirect("displayProduct.jsp"); }else{ response.sendRedirect("error.jsp"); } }catch(sqlException e){ e.printstacktrace(); } } }
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head><title>商品查询</title></head> <body> <form action = "queryproduct.do" method="post"> 请输入商品号: <input type = "text" name="productid" size="15"> <input type = "submit" value = "确定"> </form> </body> </html>
package com.demo; import java.io.Serializable; public class Product implements Serializable{ private String prod_id; private String pname; private double price; private int stock; public Product(){} public Product(String prod_id, String pname, double price, int stock) { this.prod_id = prod_id; this.pname = pname; this.price = price; this.stock = stock; } public String getProd_id() { return prod_id; } public void setProd_id(String prod_id) { this.prod_id = prod_id; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } }
错误如下:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。