Jsp页面和数据库之间的通信

时间:2018-09-07 08:46:18

标签: mysql database jsp

我正在学习编程,并且试图在页面中创建用户已完成的预订列表。 特别是,我想恢复用户使用其电子邮件进行的所有预订 我已经创建了一个JSP页面:

<body>
<%=session.getAttribute("Nome") +" "+ session.getAttribute("Cognome") + " "+ session.getAttribute("Email") %>
<br>

Prenotazioni Effettuate:

<%
try{
            String email=(String)session.getAttribute("Email");         

       // out.print("Hello "+n);
        Class.forName("com.mysql.cj.jdbc.Driver");
       // out.println("driver loaded");
     Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Hotel","root" ,"123456789");
      //  out.println("Connect");
      Statement  stmt =  con.createStatement();
      //    out.println("conncetion successfull");
        String query="";
        String emailp = "";
        String arrivdate = "";

    query="select * from reservation where email = "+email;
    ResultSet r = stmt.executeQuery(query);
    while (r.next())
    {   
    emailp = r.getString("email");
    arrivdate = r.getString("arrivalDate");
    %>
Email: <%= emailp %>

Arrival date: <%= arrivdate %>


           <% }}

            catch(Exception e)
                    {

                    }
            out.close();

            %>

我不明白为什么什么也没印出来。 是查询问题还是错误的代码问题?你知道如何帮助我吗?

GRAVE: Servlet.service() for servlet [jsp] in context with path
> [/Apartment] threw exception [java.lang.IllegalStateException:
> Exception occurred when flushing data] with root cause
> java.io.IOException: Stream closed    at
> org.apache.jasper.runtime.JspWriterImpl.ensureOpen(JspWriterImpl.java:187)
>   at
> org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:105)
>   at
> org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:180)
>   at
> org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
>   at
> org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
>   at
> org.apache.jsp.Prenotazionieffettuate_jsp._jspService(Prenotazionieffettuate_jsp.java:198)
>   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
>   at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)     at
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
>   at
> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
>   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
>   at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
>   at
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
>   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
>   at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
>   at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
>   at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
>   at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
>   at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
>   at
> org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
>   at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
>   at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
>   at
> org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:412)
>   at
> org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
>   at
> org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
>   at
> org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1385)
>   at
> org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
>   at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
>   at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
>   at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:-2)

我用这种方式解决了:

String query="SELECT * FROM reservation WHERE email= '" + session.getAttribute("Email") + "'";