我无法用JSP搜索 - Servlet - DAO

时间:2018-05-16 06:26:24

标签: jsp servlets dao

我使用JSP,Servlet,DAO和Mysql来制作列表并从数据表中搜索数据,列表有效,但我无法搜索。

我想搜索您收到duracionpeli(int)字段的两个参数的特定范围 例如: select * from tb_pelicula其中duracionpeli&gt; = 180并且duracionpeli <= 200; 在Mysql中查询有效,但在JSP中我不能这样做。 我不知道错误在哪里

MySQL的:

create database pelicula;
use pelicula;
create table tb_pelicula(
idpelicula int(11) primary key auto_increment,
nompeli varchar(50),
descripeli varchar(50),
clasipeli varchar(50),
duracionpeli int(11),
director varchar(50),
actor varchar(50),
sinopsis varchar(50)
);
insert into tb_pelicula values
(null,'Titanic','Drama','Drama',240,'James Cameroon','Leonardo Di 
Caprio','Barco Hundido'),
(null,'Avengers','Comics','Comics',200,'AAA','EEEEE','SuperHerores'),
(null,'Interstellar','CienciaFiccion','CienciaFiccion',180,'AAA','EEEEE',
'Universo');

 select * from tb_pelicula where duracionpeli>=180 and duracionpeli<=200;

豆类:

package beans;

public class Peliculas {

private int idpelicula,duracionpeli ;
private String nompeli,descripeli,clasipeli,director,actor,sinopsis;
public Peliculas() {

}


public Peliculas(int idpelicula, String nompeli, String descripeli, String clasipeli,int duracionpeli,
        String director, String actor, String sinopsis) {
    this.idpelicula = idpelicula;
    this.nompeli = nompeli;
    this.descripeli = descripeli;
    this.clasipeli = clasipeli;
    this.duracionpeli = duracionpeli;
    this.director = director;
    this.actor = actor;
    this.sinopsis = sinopsis;
}


public int getIdpelicula() {
    return idpelicula;
}
public void setIdpelicula(int idpelicula) {
    this.idpelicula = idpelicula;
}
public int getDuracionpeli() {
    return duracionpeli;
}
public void setDuracionpeli(int duracionpeli) {
    this.duracionpeli = duracionpeli;
}
public String getNompeli() {
    return nompeli;
}
public void setNompeli(String nompeli) {
    this.nompeli = nompeli;
}
public String getDescripeli() {
    return descripeli;
}
public void setDescripeli(String descripeli) {
    this.descripeli = descripeli;
}
public String getClasipeli() {
    return clasipeli;
}
public void setClasipeli(String clasipeli) {
    this.clasipeli = clasipeli;
}
public String getDirector() {
    return director;
}
public void setDirector(String director) {
    this.director = director;
}
public String getActor() {
    return actor;
}
public void setActor(String actor) {
    this.actor = actor;
}
public String getSinopsis() {
    return sinopsis;
}
public void setSinopsis(String sinopsis) {
    this.sinopsis = sinopsis;
}
}

接口:

package interfaces;

import java.util.List;
import beans.Peliculas;

public interface OperacionesDAO {
public List<Peliculas> listar();
public List<Peliculas> buscar(int id1, int id2);
}

DAOPeliculas:

package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import beans.Peliculas;
import interfaces.OperacionesDAO;
import miConexion.MysqlDBConexion;

public class DAOPeliculas implements OperacionesDAO{

@Override
public List<Peliculas> listar() {
    List<Peliculas> data = new ArrayList<Peliculas>();
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      conn = MysqlDBConexion.getConexion();
      String sql = "select * from tb_pelicula";
      pstm = conn.prepareStatement(sql);
      rs = pstm.executeQuery();
      while (rs.next()) {
        data.add(new Peliculas(rs.getInt("idpelicula"),
                rs.getString("nompeli"),
                rs.getString("descripeli"),
                rs.getString("clasipeli"),
                rs.getInt("duracionpeli"),
                rs.getString("director"),
                rs.getString("actor"),
                rs.getString("sinopsis")));
      }
    } catch (Exception e) {
        e.printStackTrace();
    } finally{
        try {
            if(rs!= null) rs.close();
            if(pstm!= null) pstm.close();
            if(conn!= null) conn.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return data;
}

@Override
public List<Peliculas> buscar(int id1, int id2) {
    List<Peliculas> data = new ArrayList<Peliculas>();
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      conn = MysqlDBConexion.getConexion();
      String sql = "select * from tb_pelicula where duracionpeli>=? AND 
duracionpeli<=?";
      pstm = conn.prepareStatement(sql);
      rs = pstm.executeQuery();
      while (rs.next()) {
        data.add(new Peliculas(rs.getInt("idpelicula"),
                rs.getString("nompeli"),
                rs.getString("descripeli"),
                rs.getString("clasipeli"),
                rs.getInt("duracionpeli"),
                rs.getString("director"),
                rs.getString("actor"),
                rs.getString("sinopsis")));
      }
    } catch (Exception e) {
        e.printStackTrace();
    } finally{
        try {
            if(rs!= null) rs.close();
            if(pstm!= null) pstm.close();
            if(conn!= null) conn.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return data;
}

}

ServletPelicula:

package controlador;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Peliculas;
import dao.DAOPeliculas;

/**
* Servlet implementation class ServletPelicula
*/
@WebServlet("/ServletPelicula")
public class ServletPelicula extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public ServletPelicula() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#service(HttpServletRequest request, 
HttpServletResponse response)
 */
protected void processRequest(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException {
    try(PrintWriter out = response.getWriter()){

        DAOPeliculas dao = new DAOPeliculas();
        List<Peliculas> data = new ArrayList<>();
        String iden1 = request.getParameter("id1");
        String iden2 = request.getParameter("id2");
        RequestDispatcher rd=null;
        try {
            if (request.getParameter("btnBuscar")!=null) {
                int dura1 = Integer.parseInt(iden1);
                int dura2 = Integer.parseInt(iden2);
                data = dao.buscar(dura1, dura2);
                request.setAttribute("filtro", data);
            }
            rd = request.getRequestDispatcher("/listado.jsp");

        } catch (Exception e) {
            e.printStackTrace();
        }
        rd.forward(request, response);
    }
}

}

和JSP listado.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="beans.Peliculas"%>
<%@page import="dao.DAOPeliculas"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Listar Alumnos</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<script type="text/javascript">
</script>
<div class="container">
        <div class="row col-lg-10">
            <h2 class="text-center">Listado de Peliculas</h2>
            <div class="panel panel-info">
                <div class="panel-heading">Filtros de Busqueda</div>
                <div class="panel-body">     
                <%
                    DAOPeliculas dao = new DAOPeliculas();
                    List<Peliculas> data = new ArrayList<>();
                %>               
                    <form action="/ServletPelicula" method="post" >
                        <div class="input-group col-lg-3" style="width: 
20%">
                            <span class="input-group-addon">Desde</span>
                            <input type="text" class="form-control" 
name="id1">
                        </div>
                        <div class="input-group col-lg-3" style="width: 
20%">
                            <span class="input-group-addon">Hasta</span>
                            <input type="text" class="form-control" 
name="id2">
                        </div>
                        <div class="input-group col-lg-3" style="width: 
20%">
                            <input type="button" class="btn btn-primary" 
name="btnBuscar" value="Buscar">
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-10">
                <div class="panel panel-success">
                    <div class="panel-heading">

                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-hover" 
id="tablaCliente" >
                            <tr>
                                <th>Cod-Pelicula</th>
                                <th>Titulo</th>
                                <th>Descripcion</th>
                                <th>Clasificacion</th>
                                <th>Duracion</th>
                                <th>Director</th>
                                <th>Actor</th>
                                <th>Sinopsis</th>
                            </tr>
                            <tr>
                                <% 

if(request.getAttribute("filtro")!=null){
                                            data =(List<Peliculas>) 
request.getAttribute("filtro");
                                        }else{
                                            data = dao.listar();
                                        }


                                        for(Peliculas x:data){      
                                %>  
                                <tr>
                                    <td><%=x.getIdpelicula() %></td>
                                    <td><%=x.getNompeli() %></td>
                                    <td><%=x.getDescripeli() %></td>
                                    <td><%=x.getClasipeli() %></td>
                                    <td><%=x.getDuracionpeli() %></td>
                                    <td><%=x.getDirector() %></td>
                                    <td><%=x.getActor() %></td>
                                    <td><%=x.getSinopsis() %></td>              
                                </tr>
                                <%

                                    }
                                %>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="panel-footer">

                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

原因是当你使用pstm来查询它时,你没有为你的sql设置参数。你的查询sql是select * from tb_pelicula where duracionpeli>=? AND duracionpeli<=?它需要设置两个参数。

要解决此问题,您需要更改buscar(int id1, int id2)的代码,如下所示:

conn = MysqlDBConexion.getConexion();
String sql = "select * from tb_pelicula where duracionpeli>=? AND 
    duracionpeli<=?";
pstm = conn.prepareStatement(sql);
pstm.setInt(1,id1);//set parameter
pstm.setInt(2,id2);//set parameter
rs = pstm.executeQuery();
while (rs.next()) {
    data.add(new Peliculas(rs.getInt("idpelicula"),
            rs.getString("nompeli"),
            rs.getString("descripeli"),
            rs.getString("clasipeli"),
            rs.getInt("duracionpeli"),
            rs.getString("director"),
            rs.getString("actor"),
            rs.getString("sinopsis")));
}

答案 1 :(得分:0)

是pstm。 setString (1,id1);或者pstm。 setInt (1,id1);?

我改变了setInt:

pstm.setInt(1,id1);//set parameter
pstm.setInt(2,id2);//set parameter

但是,我无法搜索。