我无法使用 JSP - Servlet - DAO 进行搜索
Posted
技术标签:
【中文标题】我无法使用 JSP - Servlet - DAO 进行搜索【英文标题】:I can't Search with JSP - Servlet - DAO 【发布时间】:2018-10-26 02:07:56 【问题描述】:我正在使用 JSP、Servlet、DAO 和 mysql 制作列表并从数据表中搜索数据,列表有效,但是我无法搜索。
我想搜索一个特定的范围,你接收到字段 duracionpeli (int) 的两个参数 例如: select * from tb_pelicula where duracionpeli> = 180 and duracionpeli
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>
【问题讨论】:
【参考方案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")));
【讨论】:
HI, is pstm.**setString**(1,id1);或 pstm.**setInt**(1,id1);?我是通过setInt改的:pstm.setInt(1,id1);//设置参数 pstm.setInt(2,id2);//设置参数 但是,我搜不到。 @PedroPSegura 是的,应该是pstm.setInt
【参考方案2】:
是 pstm.setString(1,id1);或 pstm.setInt(1,id1);?
我被 setInt 改变了:
pstm.setInt(1,id1);//set parameter
pstm.setInt(2,id2);//set parameter
但是,我无法搜索。
【讨论】:
以上是关于我无法使用 JSP - Servlet - DAO 进行搜索的主要内容,如果未能解决你的问题,请参考以下文章
当 Servlet、JSP 和轻量级 DAO 层可以工作时,为啥要使用 MVC 框架?
带有 Servlet、JSP 和 MySQL 的 Java 中的 CRUD Web 应用程序,没有 DAO