Java 错误500:通过servlet将数据插入数据库
我是Java中servlet等概念的初学者,最近我一直在尝试这些概念。我试图在customer(customerid、username、password)表中插入一行,customerid是自动生成的,但不幸的是,我总是遇到一个错误500。这是我的servlet、HTML文件和控制台上显示的错误消息 newUserLoginServelet.javaJava 错误500:通过servlet将数据插入数据库,java,servlets,Java,Servlets,我是Java中servlet等概念的初学者,最近我一直在尝试这些概念。我试图在customer(customerid、username、password)表中插入一行,customerid是自动生成的,但不幸的是,我总是遇到一个错误500。这是我的servlet、HTML文件和控制台上显示的错误消息 newUserLoginServelet.java package loginPackage; import java.io.IOException; import java.io.PrintWr
package loginPackage;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import resources.MyUtil;
/**
* Servlet implementation class NewUserLoginServlet
*/
@WebServlet(description = "Here a new/first time user sets up his/her login credentials.", urlPatterns = { "/NewUserLoginServlet" })
public class NewUserLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public NewUserLoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = request.getParameter("username");
String password = request.getParameter("password");
String SQL = "insert into table customer (username, password) values (" + "'" + username + "','" + password + "');" ;
Connection connection = MyUtil.getConnection(); /*Import resources.MyUtil */
PrintWriter out = response.getWriter();
response.setContentType("text/html");
try {
/*Throws SQLException*/
Statement statement = connection.createStatement();
statement.executeQuery(SQL);
out.println("New user credentials injected into the database.");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package resources;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyUtil {
private static Connection connection = null;
private static String url = "jdbc:mysql://localhost:3306/mysql";
private static String password = "*****";
private static String user = "root";
public static Connection getConnection() {
if (connection == null) {
try {
Class.forName("com.mysql.jdbc.Driver"); // ClassNotFoundException
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return (connection);
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome to the SG Supermarket.</title>
</head>
<body>
<h1>Welcome to the SG supermarket.</h1>
<a href="signIn.html">In case you are not a new user.</a></br>
<a href="signUp.html">In case you are a new user.</a>
<
/body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User sign up (First time user)</title>
</head>
<body>
<form action="NewUserLoginServlet">
Please enter the credentials you wish to be. </br>
Username : <input type="text" value="username"> </br>
Password : <input type="password" value="password"> </br>
<input type="submit" value="Submit your credentials."> </br>
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SGCart2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description>NewUserLoginServlet</description>
<display-name>NewUserLoginServlet</display-name>
<servlet-name>NewUserLoginServlet</servlet-name>
<servlet-class>loginPackage.NewUserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewUserLoginServlet</servlet-name>
<url-pattern>/NewUserLoginServlet</url-pattern>
</servlet-mapping>
</web-app>
错误消息:
Type Exception Report
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException
loginPackage.NewUserLoginServlet.doGet(NewUserLoginServlet.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
public class MyUtil {
private static Connection conn = null;
private static String url = "jdbc:mysql://localhost:3306/mysql";
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "*****");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
<servlet>
<description>NewUserLoginServlet</description>
<display-name>NewUserLoginServlet</display-name>
<servlet-name>NewUserLoginServlet</servlet-name>
<servlet-class>loginPackage.NewUserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewUserLoginServlet</servlet-name>
<url-pattern>/NewUserLoginServlet</url-pattern>
</servlet-mapping>
在数据库中插入数据是否有问题,或者在HTML和servlet之间创建连接失败?任何帮助都将不胜感激
我的数据库URL的屏幕截图
web.xml
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome to the SG Supermarket.</title>
</head>
<body>
<h1>Welcome to the SG supermarket.</h1>
<a href="signIn.html">In case you are not a new user.</a></br>
<a href="signUp.html">In case you are a new user.</a>
<
/body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User sign up (First time user)</title>
</head>
<body>
<form action="NewUserLoginServlet">
Please enter the credentials you wish to be. </br>
Username : <input type="text" value="username"> </br>
Password : <input type="password" value="password"> </br>
<input type="submit" value="Submit your credentials."> </br>
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SGCart2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description>NewUserLoginServlet</description>
<display-name>NewUserLoginServlet</display-name>
<servlet-name>NewUserLoginServlet</servlet-name>
<servlet-class>loginPackage.NewUserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewUserLoginServlet</servlet-name>
<url-pattern>/NewUserLoginServlet</url-pattern>
</servlet-mapping>
</web-app>
SGCart2
index.html
NewUserLoginServlet
NewUserLoginServlet
NewUserLoginServlet
loginPackage.NewUserLoginServlet
NewUserLoginServlet
/NewUserLoginServlet
此sql语句不正确:
String SQL = "insert into table customer (username, password) values (" + "'" + username + "','" + password + "');" ;
应该是:
String SQL = "insert into customer (username, password) values ('" + username + "','" + password + "');" ;
你也应该考虑做一个PraveReDATA语句,它要简单得多,你不必在使用双+单引号的情况下混在一起,如果你的INSERT语句大得多,这可能会非常混乱:
String username = request.getParameter("username");
String password = request.getParameter("password");
try(Connection conn= MyUtil.getConnection()){
PreparedStatement pst = conn.prepareStatement("insert into customer (username,password) values (?,?);");
pst.setString(1, username);
pst.setString(2, password);
pst.executeUpdate();
}catch (SQLException e) {
e.printStackTrace();
}
无论何时插入、更新或删除,都必须使用.executeUpdate()而不是。executeQuery()强>
此外,我还建议查看MVC体系结构。(模型视图控制器)。。。在servlet中执行数据库命令不是很好的做法。出于安全和组织的原因,您应该将此逻辑分开
您得到的错误是NewUserLoginServlet中第51行的NullPointerExection。这意味着这一行是错误的:
Statement statement = connection.createStatement();
在DBConnection类(MyUtil)中删除此括号:
应该是:
return connection;
编辑:尝试将其作为您的数据库连接类:
Type Exception Report
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException
loginPackage.NewUserLoginServlet.doGet(NewUserLoginServlet.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
public class MyUtil {
private static Connection conn = null;
private static String url = "jdbc:mysql://localhost:3306/mysql";
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "*****");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
<servlet>
<description>NewUserLoginServlet</description>
<display-name>NewUserLoginServlet</display-name>
<servlet-name>NewUserLoginServlet</servlet-name>
<servlet-class>loginPackage.NewUserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewUserLoginServlet</servlet-name>
<url-pattern>/NewUserLoginServlet</url-pattern>
</servlet-mapping>
编辑2:在web.xml中,应该有如下内容:
Type Exception Report
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException
loginPackage.NewUserLoginServlet.doGet(NewUserLoginServlet.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
public class MyUtil {
private static Connection conn = null;
private static String url = "jdbc:mysql://localhost:3306/mysql";
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "*****");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
<servlet>
<description>NewUserLoginServlet</description>
<display-name>NewUserLoginServlet</display-name>
<servlet-name>NewUserLoginServlet</servlet-name>
<servlet-class>loginPackage.NewUserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewUserLoginServlet</servlet-name>
<url-pattern>/NewUserLoginServlet</url-pattern>
</servlet-mapping>
NewUserLoginServlet
NewUserLoginServlet
NewUserLoginServlet
loginPackage.NewUserLoginServlet
NewUserLoginServlet
/NewUserLoginServlet
在表单上尝试添加method=“GET”@Austin不起作用。不知道出了什么问题。您是否已将servlet添加到web.xmlNo。我的印象是web.xml已经引用了index.html,这反过来又导致了我的servlet。看看这篇文章,它在解释如何设置servlet方面做得很好。我按照您的要求做了更改,但是我得到了一个空白屏幕,我的数据库也没有更新。我再也没有收到任何错误了。@abhijeet在您的web.xml文件中,您的newuserlogin servlet映射到了什么?实际上什么都没有。它从index.html@JonathanAgain开始,你不应该把它放在。。。。该部分仅适用于用户在访问您的站点时首先查看的jsp/html。。。如果该文件中没有servlet映射,那么servlet将无法工作。就这么简单。请记住重新启动服务器并清理项目/确保所有信息正确无误,否则会出错。是的,我做了@乔纳森