在查询中将参数从Servlet传递到Oracle

时间:2019-02-06 22:19:25

标签: jsp oracle10g

我想从jsp恢复日期(日期和月份)和电影院后,通过sql查询将这些参数传输到数据库oracle。通过调试检查,查询不会产生任何类型的错误,但是同时(result.next())不会填充3个数组(filmArray,roomArray和dataArray)。 我在 else if(Cinema!= null && month!= null && day!= null){

很明显,对于我输入的参数,通过查询可以得到一些结果,但未显示... 我在所有其他servlet上都遇到这种问题,我可以进行插入或仅选择不带where子句,因此只能用于简单查询。

package cinema;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Calendar;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class ScheduleCinema
 */
@WebServlet("/ScheduleCinema")
public class ScheduleCinema extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScheduleCinema() {
        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 user = "cinema";
        String pass = "cinema";
        String sid = "orcl";
        String host = "localhost";
        int port = 1521;
        response.getWriter().append("Served at: ").append(request.getContextPath());
        String url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
        System.out.println(url);
        ArrayList<String> CinemaArray = new ArrayList<>();
        String Cinema=request.getParameter("cinema");
        String day= request.getParameter("day");
        String month= request.getParameter("month");
        String year= request.getParameter("year");

        //String data2 = day +"-"+ month +"-"+ year + " 11:00:00 PM";
        ArrayList<String> dataArray = new ArrayList<>();
        ArrayList<String> filmArray = new ArrayList<>();
        ArrayList<String> roomArray = new ArrayList<>();


        try 
        {
            if(Cinema == null){
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection conn = DriverManager.getConnection(url, user, pass);
            PreparedStatement pstmt=conn.prepareStatement("select nome from cinema_tab");

            ResultSet result= pstmt.executeQuery();
            response.setContentType("text/html");

            PrintWriter out= response.getWriter(); 

            while (result.next()) 
            {
                CinemaArray.add(result.getString("nome"));
            }

            request.setAttribute("cinemaArray", CinemaArray);
            result.close();
            pstmt.close();
            conn.close();
            request.getRequestDispatcher("/ScheduleCinema.jsp").forward(request, response);


            }else if(Cinema != null && month != null && day != null){

                CinemaArray.clear();
                CinemaArray.add(Cinema);
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection conn = DriverManager.getConnection(url, user, pass);
                PreparedStatement pstmt=conn.prepareStatement("select title, room, datahour from view_palinstesto_hour  where mon=? and dd=? and nomecinema=?");
                pstmt.setString(1, month);
                pstmt.setString(2, day);
                pstmt.setString(3, Cinema);
                ResultSet result= pstmt.executeQuery();
                //response.setContentType("text/html");

                response.getWriter(); 

                while (result.next()) 
                {
                    filmArray.add(result.getString("title"));
                    dataArray.add(result.getString("datahaur"));
                    roomArray.add(result.getString("room"));
                }


                request.setAttribute("cinemaArray", CinemaArray);
                request.setAttribute("filmArray", filmArray);
                request.setAttribute("dataArray", dataArray);
                request.setAttribute("roomArray", roomArray);


                result.close();
                pstmt.close();
                conn.close();
                request.getRequestDispatcher("/ScheduleCinema.jsp").forward(request, response);
            }
        }
        catch(ClassNotFoundException e){
            throw new ServletException(e);
         }
        catch(SQLException e){
            throw new ServletException(e);
         }
    }

    /**
     * @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);
    }

}

我真的无法弄清楚问题出在哪里以及在哪里。 如果我还要输入相应的JSP页面的代码。

<%@page
    import="jdk.nashorn.internal.objects.annotations.SpecializedFunction"%>
<%@page import="java.util.ArrayList"%>
<%@page import="cinema.ScheduleCinema"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.util.Calendar" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Schedule | Cinema </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Place your description here" />
<meta name="keywords" content="put, your, keyword, here" />
<meta name="author" content="Templates.com - website templates provider" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/cufon-yui.js" type="text/javascript"></script>
<script src="js/cufon-replace.js" type="text/javascript"></script>
<script src="js/Gill_Sans_400.font.js" type="text/javascript"></script>
<script src="js/script.js" type="text/javascript"></script>
<!--[if lt IE 7]>
    <script type="text/javascript" src="js/ie_png.js"></script>
    <script type="text/javascript">
         ie_png.fix('.png, .link1 span, .link1');
    </script>
    <link href="ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
</head>
<body id="page5">
<div class="tail-top">
    <div class="tail-bottom">
        <div id="main">
<!-- HEADER -->
            <div id="header">
                <div class="row-1">
                    <div class="fleft"><a href="index.html">Cinema <span>World</span></a></div>
                    <ul>
                        <li><a href="index.html"><img src="images/icon1.gif" alt="" /></a></li>
                        <li><a href="contact-us.html"><img src="images/icon2-act.gif" alt="" /></a></li>
                        <li><a href="sitemap.html"><img src="images/icon3.gif" alt="" /></a></li>
                    </ul>
                </div>
                <div class="row-2">
                    <ul>
                        <li><a href="index.html">Index</a></li>
                        <li><a href="Ticket" >Ticket</a></li>
                        <li><a href="InsertFilm.jsp" >InsertFilm</a></li>
                        <li><a href="NewFilmScreening" >NewScreening</a></li>
                        <li><a href="SearchScreening" >Search Screening</a></li>
                        <li><a href="ScheduleCinema"  class="active" >Schedule Cinema</a></li>

                    </ul>
                </div>
            </div>
<!-- CONTENT -->
<br>
<br></br></br>
            <div id="content">
                <div class="line-hor"></div>
                <div class="box">
                    <div class="border-right">
                        <div class="border-left">
                            <div class="inner">
                                <h3><span> Search Schedule</span></h3>

                            </div>
                        </div>
                    </div>
                </div>
                <div class="content">
                        <%
                        ArrayList<String>cinema=(ArrayList<String>) request.getAttribute("cinemaArray");

                        // print the information about every category of the list
                        if(cinema.size()>1){
                        %>
                            <form id="cinemaSelection" action="ScheduleCinema" >
                            <fieldset>

                            <div class="field"><label><br></br>Select Cinema: </label>
                            <select name="cinema"  >


                            <option> Select Cinema </option>
                            <%
                            for(int k= 0; k < cinema.size() ; k++) {

                            %>

                            <option value="<% out.println(cinema.get(k)); %>"> <% out.println(cinema.get(k)); %></option>
                            <%    

                            }


                        %>
                        </select>
                            <label> Day ->
                             </label>
                             <select name="day">
                                <%
                                Calendar now = Calendar.getInstance();
                                for(int k=1; k<= 31 ; k++){
                                %>
                                    <option value= <% out.println(k); %>> <% out.println(k); %></option>
                                <%
                                }
                                %>

                             </select>
                             <label> Month ->
                             </label>
                             <select name="month" value=<% out.println(now.get(Calendar.MONTH)); %>>

                                    <option value="1"> 1 </option>
                                    <option value="2"> 2 </option>
                                    <option value="3"> 3 </option>
                                    <option value="4"> 4 </option>
                                    <option value="5"> 5 </option>
                                    <option value="6"> 6 </option>
                                    <option value="7"> 7 </option>
                                    <option value="8"> 8 </option>
                                    <option value="9"> 9 </option>
                                    <option value="10"> 10 </option>
                                    <option value="11"> 11 </option>
                                    <option value="12"> 12 </option>


                             </select>
                              <label> Year ->
                             </label>
                             <select name="year" value=<% out.println(now.get(Calendar.YEAR)); %>>
                                <%
                                for(int k=2019; k<= 2020  ; k++){
                                %>
                                    <option value= <% out.println(k); %>> <% out.println(k); %></option>
                                <%
                                }
                                %>

                             </select>
                             <br></br>
                              <input type="reset"  value="Resetta il form">
                              <input type="submit" value="Invia">
                        </div>
                        </fieldset>
                    </form>
                    <%
                        }else if( cinema.size()==1){
                            ArrayList<String>data=(ArrayList<String>) request.getAttribute("dataArray");
                            ArrayList<String>film=(ArrayList<String>) request.getAttribute("filmArray");
                            ArrayList<String>room=(ArrayList<String>) request.getAttribute("roomArray");


                            %>
                            <table>
                            <tr>
                            <td><h3>Room</h3></td> <td></td> <td><h3>Film </h3></td> <td></td> <td><h3>Data </h3></td>
                            </tr>
                            <%
                            for(int i=0; i< data.size();i++){
                                %>
                                <tr><td><% out.println(room.get(i));%></td> <td></td> <td><% out.println(film.get(i));%></td> <td></td> <td><% out.println(data.get(i));%></td></tr>
                                <%
                            }
                            %>

                            </table>
                            <br></br>
                            <div class="wrapper">
                            <a href="SearchScreening" class="link2">    
                                <span>
                                    <span>Reset</span>
                                </span>
                            </a>
                            </div>

                            <%
                        }
                    %>
                </div>

                </div>

            </div>
<!-- FOOTER -->
            <div id="footer">
                <div class="left">
                    <div class="right">
                        <div class="inside">Copyright - Type in your name here<br />
                            Website template designed by <a class="new_window" href="http://www.templatemonster.com/" target="_blank" rel="nofollow">www.templatemonster.com</a><br />
                            3D Models provided by <a class="new_window" href="http://www.templates.com/product/3d-models/" target="_blank" rel="nofollow">www.templates.com</a> 
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript"> Cufon.now(); </script>
</body>
</html>

0 个答案:

没有答案