Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用servlet和jquery搜索数据库_Java_Jquery_Database_Search_Servlets - Fatal编程技术网

Java 用servlet和jquery搜索数据库

Java 用servlet和jquery搜索数据库,java,jquery,database,search,servlets,Java,Jquery,Database,Search,Servlets,我目前正在从事电影项目,我一直在从数据库中搜索电影。 所以,我的主页上有一张电影表 <div id="moviesTable"> <div class="col-md-2 text-center" id="moviesSearchBox"> <div class="col-md-3 text-center" id="searchOptions"> <select id="searc

我目前正在从事电影项目,我一直在从数据库中搜索电影。 所以,我的主页上有一张电影表

<div id="moviesTable">

        <div class="col-md-2 text-center" id="moviesSearchBox">
            <div class="col-md-3 text-center" id="searchOptions">
                <select id="searchOptionsBox" name="searchOptionsBox" class="browser-default custom-select">
                    <option value="title">Title</option>
                    <option value="distributor">Distributor</option>
                    <option value="originCountry">Origin country</option>
                    <option value="yearOfProduction">Year of production</option>
                </select>
            </div>
            <div id="searchMovieInput">
                <input id="searchMovieInputBox" type="text" class="form-control">
            </div>
            <div id="searchMovieBtnBox">
                <button class="btn btn-primary" type="submit" id="searchMovieBtn">Search</button>
            </div>
        </div>  
        <table id="moviesTable1" class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>Duration</th>
                    <th>Distributor</th>
                    <th>Origin country</th>
                    <th>Year of production</th>
                </tr>       
            </thead>
            <tbody id="moviesTbody">

            </tbody>
        </table>

    </div>

我很清楚,$get()中有错误,但我想不通,我被卡住了,非常感谢您的帮助。

为什么不打开浏览器的开发人员工具,看看页面上出现了什么异常?我只能在eclipse控制台中看到错误:严重:Servlet.service()对于servlet SearchMoviesServlet抛出异常java.lang.NumberFormatException:对于输入字符串:“undefined”,但我不知道如何将输入值包含到整个内容中首先在变量中创建该查询,以便您可以在控制台中编写输出以进行调试,然后仅在$.get中使用
public static List<Movie> searchMovies(String title, String distributor, String originCountry, Integer yearOfProduction) {
        List<Movie> movies = new ArrayList<>();

        Connection con = ConnectionManager.getConnection();

        PreparedStatement ps = null;
        ResultSet rs = null;

        String query = "SELECT * FROM movies WHERE";

        if (title != null && title !="") {
            query += " title LIKE ?";
        }
        if (distributor != null && distributor !="") {
            query += " distributor LIKE ?";
        }
        if (originCountry != null && originCountry !="") {
            query += " origincountry LIKE ?";
        }
        if (yearOfProduction != null && yearOfProduction !=0) {
            query += " yearofproduction = ?";
        }

        try {
            ps = con.prepareStatement(query);
            int index = 1;

            if (title != null && title !="") {
                ps.setString(index++, title);
            }
            if (distributor != null && distributor !="") {
                ps.setString(index++, distributor);
            }
            if (originCountry != null && originCountry !="") {
                ps.setString(index++, originCountry);
            }
            if (yearOfProduction != null) {
                ps.setInt(index++, yearOfProduction);
            }

            rs = ps.executeQuery();
            while (rs.next()) {
                index = 1;
                int id = rs.getInt(index++);
                String title_rs = rs.getString(index++);
                String duration = rs.getString(index++);
                String distributor_rs = rs.getString(index++);
                String originCountry_rs = rs.getString(index++);
                int yearOfProduction_rs = rs.getInt(index++);


                Movie movie = new Movie();
                movie.setId(id);
                movie.setTitle(title_rs);
                movie.setDuration(duration);
                movie.setDistributor(distributor_rs);
                movie.setOriginCountry(originCountry_rs);
                movie.setYearOfProduction(yearOfProduction_rs);

                movies.add(movie);

            }

        } catch (Exception e) {
            System.out.println("SQL query error!");
            e.printStackTrace();
        } finally {
            try {ps.close();} catch (Exception ex1) {ex1.printStackTrace();}
            try {rs.close();} catch (Exception ex1) {ex1.printStackTrace();}
        }

        return movies;
    }
public class SearchMoviesServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String searchOptionBox = request.getParameter("searchOptionBox");
        String title = request.getParameter("title");
        String distributor = request.getParameter("distributor");
        String originCountry = request.getParameter("originCountry");
        Integer yearOfProduction = Integer.parseInt(request.getParameter("yearOfProduction"));

        List<Movie> movies = MovieDAO.searchMovies(title, distributor, originCountry, yearOfProduction);
        if(searchOptionBox != null) {
            switch(searchOptionBox) {
                case "title":
                    Collections.sort(movies, Movie.titleComparator);
                    break;
                case "distributor":
                    Collections.sort(movies, Movie.distributorComparator);
                    break;
                case "originCountry":
                    Collections.sort(movies, Movie.originCountryComparator);
                    break;
                case "yearOfProduction":
                    Collections.sort(movies, Movie.yearOfProductionComparator);
                    break;
                default:
                    break;
                }   
        }

        Map<String, Object> data = new HashMap<>();
        data.put("movies", movies);

        ObjectMapper mapper = new ObjectMapper();
        String jsonData = mapper.writeValueAsString(data);
        System.out.println(jsonData);

        response.setContentType("application/json");
        response.getWriter().write(jsonData);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
searchMovies : function() {
            $('#moviesTable1').dataTable().fnClearTable();
            $.get('SearchMoviesServlet?searchOptionBox=' + $("searchOptionBox option:selected").val() + '&title='
                    + $("#title").val() + "&distributor=" + $("#distributor").val() + "&originCountry="
                    + $("#originCountry").val() + "&yearOfProduction=" + $("#yearOfProduction").val(), function(data){
                for(m in data.movies) {
                    $('#moviesTable1').dataTable().fnAddData ([
                        data.movies[m].title,
                        data.movies[m].duration,
                        data.movies[m].distributor,
                        data.movies[m].originCountry,
                        data.movies[m].yearOfProduction 
                        ]);
                }
            });


        }