我正在尝试打印一个字符串,该字符串是从数据库中获取并将其保存在结果集变量中。但是当我尝试执行它时,它总是显示异常
try{
rs=stmt.executeQuery("select * from users where user_id='"+username+"'");
}
catch(Exception e)
{
System.out.println(e);
}
%>
<html>
<body style="background-color:powderblue">
<%=username%>'s profile<br>
Username : <%=rs.getString("user_id")%><br>
Gender : <%=rs.getString("g")%><br>
Password : <%=rs.getString("user_password")%><br>
First Name : <%=rs.getString("f_name")%><br>
Last Name : <%=rs.getString("l_name")%><br>
</body>
</html>
我收到的错误消息是
Type Exception Report
Message An exception occurred processing JSP page [/myprofile.jsp] at line [26]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing JSP page [/myprofile.jsp] at line [26]
23: <html>
24: <body style="background-color:powderblue">
25: <%=username%>'s profile<br>
26: <%=rs.getString("user_id")%>
27: </body>
28: </html>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
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)
根源
javax.servlet.ServletException: java.sql.SQLException: ResultSet.next 未被调用 org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909( org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838( org.apache.jsp.myprofile_jsp._jspService(myprofile_jsp.java:155( 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(
您需要添加if (rs.next()) {}
或while (rs.next()) {}
。
<%
if (rs.next()) {
%>
Username : <%=rs.getString("user_id")%><br>
Gender : <%=rs.getString("g")%><br>
Password : <%=rs.getString("user_password")%><br>
First Name : <%=rs.getString("f_name")%><br>
Last Name : <%=rs.getString("l_name")%><br>
<%
}
%>