mysql 我在servlet准备语句中得到空指针异常[重复]

x8diyxa7  于 2023-08-02  发布在  Mysql
关注(0)|答案(1)|浏览(89)

此问题在此处已有答案

What is a NullPointerException, and how do I fix it?(12个回答)
13天前关闭
java.lang.NullPointerException:无法调用“java.sql.PreparedStatement.setString(int,String)”,因为“this.statement”为null
Registartion.java

package in.adit.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Registration")
public class Registration extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String URL = "jdbc:mysql://localhost:3306/user";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";

    Connection connection = null;
    PreparedStatement statement = null;

    public Registration() {
        super();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("Connect established succesfully");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        String un = request.getParameter("username");
        String pass = request.getParameter("password");
        String firstName = request.getParameter("password");
        String lastName = request.getParameter("password");
        String email = request.getParameter("password");
        String m = request.getParameter("mobile");
        System.out.println(m);
        Long mobile = Long.parseLong(m);

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();     
        try {
            String query2 = "INSERT INTO login_tbl VALUES (?,?)";
            //statement = connection.prepareStatement(query2);
            statement.setString(1,un);
            statement.setString(2,pass);
            statement.execute(query2);
            System.out.println("Query 2 Executed...");
            
            String query = "INSERT INTO user_info_tbl VALUES (?,?,?,?,?)";
            //statement = connection.prepareStatement(query);
            statement.setString(1, un);
            statement.setString(2, firstName);
            statement.setString(3, lastName);
            statement.setString(4, email);
            statement.setLong(5, mobile);
            statement.execute(query);
            System.out.println("Query 1 Executed...");
            
            response.sendRedirect("login.jsp");
        } catch (SQLException e) {
            out.println(e);
            e.printStackTrace();
        }

    }

}

个字符
我尝试查询直接把在mySql数据库在xamp这里是我的代码它的工作,所以查询是正确的,并获得方法在jsp显示所有参数传递的url

qyyhg6bp

qyyhg6bp1#

在设置任何参数之前,您需要创建Statement对象,请取消对该行的注解

//statement = connection.prepareStatement("Your SQL Query here");

字符串
只有在这之后你才能用

statement.setString

相关问题