JSP - 在Eclipse中,使用MySQL后端获取多个错误

时间:2017-05-23 12:37:44

标签: java mysql eclipse jsp

尝试使用MySQL创建简单登录。我收到了很多错误,想知道你们是否可以提供帮助。我是JSP和编程的超级新手(DBA by trade)。我在行上添加了错误,当我尝试在Eclipse中运行代码时,我得到错误500。

    <%@ page import="java.sql.Date, java.sql.PreparedStatement, java.sql.ResultSet, java.sql.SQLException, java.sql.Statement, java.sql.Timestamp, java.text.SimpleDateFormat, 
java.util.ArrayList, java.util.Calendar, java.util.HashMap, java.util.List, java.util.Map, java.util.Map.Entry, java.sql.*" %>


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Login</title>

</head>

<body>

<%! String userdbName;

String userdbPsw;

String dbUsertype;

%>

<%

Connection con= null; //Connection cannot be resolved to a type

PreparedStatement ps = null; //PreparedStatement cannot be resolved to a type

ResultSet rs = null; //ResultSet cannot be resolved to a type



String driverName = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://localhost:3306/record";

String user = "root";

String dbpsw = "root";



String sql = "select * from userdetail where name=? and password=? and usertype=?";



String name = request.getParameter("name");

String password = request.getParameter("password");

String usertype = request.getParameter("usertype");



if((!(name.equals(null) || name.equals("")) && !(password.equals(null) || 
password.equals(""))) && !usertype.equals("select"))

{

try{

Class.forName(driverName);
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, dbpsw); //DriverManager cannot be resolved (x2?)

ps = con.prepareStatement(sql);

ps.setString(1, name);

ps.setString(2, password);

ps.setString(3, usertype);

rs = ps.executeQuery();

if(rs.next())

{ 

userdbName = rs.getString("name");

userdbPsw = rs.getString("password");

dbUsertype = rs.getString("usertype");

if(name.equals(userdbName) && password.equals(userdbPsw) && usertype.equals(dbUsertype))

{

session.setAttribute("name",userdbName);

session.setAttribute("usertype", dbUsertype); 

response.sendRedirect("welcome.jsp"); 

} 

}

else

response.sendRedirect("error.jsp");

rs.close();

ps.close(); 

}

catch(SQLException sqe) //SQLException cannot be resolved to a type (x2)

{

out.println(sqe);

} 

}

else

{

%>

<center><p style="color:red">Error In Login</p></center>

<% 

getServletContext().getRequestDispatcher("/home.jsp").include(request, 
response);

}

%>

</body>

</html>

此代码返回以下错误:

  

HTTP状态500 - 内部服务器错误

     

类型例外报告

     

消息在行处理JSP页面/Login.jsp时发生异常   56

     

描述服务器遇到意外情况   阻止它履行请求。

     

异常org.apache.jasper.JasperException:发生异常   在第56行处理JSP页面/Login.jsp

     

53:54:55:56:if((!(name.equals(null)|| name.equals(&#34;&#34;))&amp;&amp;   !(password.equals(null)|| 57:password.equals(&#34;&#34;)))&amp;&amp;   !usertype.equals(&#34; select&#34;))58:59:{

     

堆栈跟踪:     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

     

根本原因java.lang.NullPointerException     org.apache.jsp.Login_jsp._jspService(Login_jsp.java:192)     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

     

注意服务器中提供了根本原因的完整堆栈跟踪   日志。

     

Apache Tomcat / 8.5.14

1 个答案:

答案 0 :(得分:0)

它的导入问题,例如在JSP中导入java类 <%@ page import="java.util.List" %>或多次导入使用

<%@ page import="java.util.List,java.util.Date,....,java.util.ArrayList" %>

希望这有帮助。