提问人:Divyesh Kuchhadia 提问时间:7/20/2023 最后编辑:user207421Divyesh Kuchhadia 更新时间:7/20/2023 访问量:99
我在 servlet 准备语句 [duplicate] 中收到空指针异常
I am getting null pointer exception in servlet prepared statement [duplicate]
问:
java.lang.NullPointerException:无法调用“java.sql.PreparedStatement.setString(int, String)”,因为“this.statement”为空
注册.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();
}
}
}
Sigup.html
<%@include file="components/header.jsp" %>
<%@include file="components/navbar.jsp" %>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="card">
<div class="card-header bg-danger">Create Account</div>
<div class="card-body">
<form action="Registration" method="get">
<table class="table table-borderless">
<tr>
<td><input type="text" class="form-control" name="username"
placeholder="Enter Username" /></td>
</tr>
<tr>
<td><input type="password" class="form-control"
name="password" placeholder="Enter Password" /></td>
</tr>
<tr>
<td><input type="text" class="form-control" name="firstname"
placeholder="Enter Firstname" /></td>
</tr>
<tr>
<td><input type="text" class="form-control" name="lastname"
placeholder="Enter Lastname" /></td>
</tr>
<tr>
<td><input type="email" class="form-control" name="email"
placeholder="Enter Email" /></td>
</tr>
<tr>
<td><input type="tel" class="form-control" name="mobile"
placeholder="Mobile Number 10 digit" pattern="[0-9]{10}" /></td>
</tr>
<tr>
<td><input type="submit" class="btn btn-danger"
value="Create Account" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
<%@include file="components/footer.jsp" %>0
我尝试将查询直接放入 xamp 中的 mySql 数据库,这是我的代码,它有效,因此查询是正确的,并且在 jsp 中获取方法显示所有参数传入 url
答:
0赞
Abdul Mohsin
7/20/2023
#1
您需要在设置任何参数之前创建 Statement 对象,请取消注释该行
//statement = connection.prepareStatement("Your SQL Query here");
只有在此之后,您才能使用
statement.setString
评论
0赞
Divyesh Kuchhadia
7/20/2023
我取消了注释,并在以下代码中删除了 null Connection connection = null;PreparedStatement 语句 = null;并且此错误显示无法调用“java.sql.Connection.prepareStatement(String)”,因为“this.connection”为空
评论
PreparedStatement statement = null;
-是的,它是。