数据库
首页 > 数据库> > (待解决)IDEA配置JDBC查询数据库PreparedStatement pstmt = dbconn.prepareStatement(sql)出现空指针错误

(待解决)IDEA配置JDBC查询数据库PreparedStatement pstmt = dbconn.prepareStatement(sql)出现空指针错误

作者:互联网

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;
    }
}

错误如下:

 

标签:PreparedStatement,String,prepareStatement,IDEA,public,pname,prod,id,stock
来源: https://www.cnblogs.com/massionter/p/11074729.html