所以,我试图通过检查一个用户是否已经注册来验证他。为此,我使用mysql,我的java文件register.java位于com.aayus.jsp包中。这是文件的一个片段
寄存器.java
package com.aayush.jsp;
import java.sql.*;
public class Register {
public static int check_if_already_exists(String id) {
ConnectToMySQL connection = new ConnectToMySQL();
connection.connect();
try {
ResultSet rs = connection.stmnt.executeQuery("SELECT id FROM login");
while(rs.next()) {
if(id==rs.getString("coumn_name")) {
return -1;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public static void main(String[] args) {
System.out.println(check_if_already_exists("101"));
}
}
以下是connecttomysql.java文件:
package com.aayush.jsp;
import java.sql.*;
public class ConnectToMySQL {
Statement stmnt = null;
public void connect() {
try {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/un","root","hellomoto");
stmnt = myConn.createStatement();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ConnectToMySQL conn = new ConnectToMySQL();
conn.connect();
}
}
如您所见,该文件使用在同一个包中定义的类connecttomysql。当作为java应用程序运行时,此代码运行良好。
问题是,当我从jsp文件调用check \u if \u already \u exists()方法时,如下所示:
<html>
<body>
<% int checkMail = com.aayush.jsp.Register.check_if_already_exists("101");%>
</body>
</html>
apache tomcat出现以下错误:
type Exception report
message An exception occurred processing JSP page /RegisterOrLogin.jsp at line 9
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /RegisterOrLogin.jsp at line 4
1:
2: <html>
3: <body>
4: <% int checkMail = com.aayush.jsp.Register.check_if_already_exists("101");%>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:579)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
com.aayush.jsp.Register.check_if_already_exists(Register.java:9)
org.apache.jsp.RegisterOrLogin_jsp._jspService(RegisterOrLogin_jsp.java:120)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
暂无答案!
目前还没有任何答案,快来回答吧!