I am creating a loan management in JSP and well, I have a problem with the list of loans.
I have not yet created a method to insert them, I have entered a loan into the database directly using MySQL to first check the method of listing the loans that are registered.
The problem there is that when I launch the application I get the following error that I attached to the screen with an image.
[![enter image description here][1]][1]
This is the menu where I click Complete List
[![enter image description here][2]][2]
This is the error that occurs.
I am attaching now the materials that I am using for this specific topic.
1º- The Loan class, which is in a Package called Entities .
package Entidades;
import java.sql.Date;
/**
*
* @author AlbertoPortatil
*/
public class Prestamo {
int id;
String DNIusuario = null;
String ISBNobra = null;
Date Fechasolicitud = null;
Date Fechadevolucion = null;
public Prestamo(int id, String DNIusuario, String ISBNobra,
Date Fechasolicitud, Date Fechadevolucion) {
this.id = id;
this.DNIusuario = DNIusuario;
this.ISBNobra = ISBNobra;
this.Fechasolicitud = Fechasolicitud;
this.Fechadevolucion = Fechadevolucion;
}
public Prestamo(String DNIusuario, String ISBNobra) {
this.DNIusuario = DNIusuario;
this.ISBNobra = ISBNobra;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDNIusuario() {
return DNIusuario;
}
public void setDNIusuario(String DNIusuario) {
this.DNIusuario = DNIusuario;
}
public String getISBNobra() {
return ISBNobra;
}
public void setISBNobra(String ISBNobra) {
this.ISBNobra = ISBNobra;
}
public Date getFechasolicitud() {
return Fechasolicitud;
}
public void setFechasolicitud(Date Fechasolicitud) {
this.Fechasolicitud = Fechasolicitud;
}
public Date getFechadevolucion() {
return Fechadevolucion;
}
public void setFechadevolucion(Date Fechadevolucion) {
this.Fechadevolucion = Fechadevolucion;
}
}
2º- The DAO class, where I attach the specific select for the loans.
package Entidades;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class DAO {
public ArrayList<Prestamo> selectPrestamo() throws Exception {
ArrayList<Prestamo> prestamos = null;
try (Connection connection = DBConnection.get()) {
if (connection == null) {//si la conexión es nula lanza una excepción
throw new Exception("Connection is null");
}
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM prestamo");
prestamos = new ArrayList();
while (rs.next()) {
Prestamo prestamo = new Prestamo(rs.getInt("id"), rs.getString("DNIusuario"),
rs.getString("ISBNobra"), rs.getDate("Fechasolicitud"),
rs.getDate("Fechadevolucion"));
prestamos.add(prestamo);
}
} catch (Exception ex) {
throw ex;
} finally {
DBConnection.close();
}
return prestamos;
}
}
3º- The SQL, the element that interests is the table Loans
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `prestamo`
--
CREATE TABLE `prestamo` (
`id` int(11) NOT NULL,
`DNIusuario` varchar(9) NOT NULL,
`ISBNobra` varchar(13) NOT NULL,
`Fechasolicitud` date NOT NULL,
`Fechadevolucion` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `prestamo`
--
INSERT INTO `prestamo` (`id`, `DNIusuario`, `ISBNobra`, `Fechasolicitud`, `Fechadevolucion`) VALUES
(1, '11111111A', '11111111A1', '2019-02-27', '2019-02-28');
-- --------------------------------------------------------
--
-- Indices de la tabla `prestamo`
--
ALTER TABLE `prestamo`
ADD PRIMARY KEY (`id`),
ADD KEY `prestamo_ibfk_1` (`DNIusuario`),
ADD KEY `prestamo_ibfk_2` (`ISBNobra`);
--
-- Filtros para la tabla `prestamo`
--
ALTER TABLE `prestamo`
ADD CONSTRAINT `prestamo_ibfk_1` FOREIGN KEY (`DNIusuario`) REFERENCES `usuario` (`DNI`) ON UPDATE CASCADE,
ADD CONSTRAINT `prestamo_ibfk_2` FOREIGN KEY (`ISBNobra`) REFERENCES `obra` (`ISBN`) ON UPDATE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
And here is the loan table with a record that I have entered to test the select.
[![enter image description here][3]][3]
4º- The jsp sheet "HojaPrestamo", where I link to another sheet called formListarPrestamo.jsp, which is the one that makes the list.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HojaUsuarios</title>
<link href="css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="titulo">
<h1>BIBLIOTECA - PRESTAMOS</h1>
<div id="menu">
<div id="opciones">
<h2><a href="index.jsp">Inicio</a></h2> <!--OK-->
<h2><a href="formListarPrestamo.jsp">Listado Completo</a></h2> <!--HACER-->
</div>
</div>
</div>
</body>
</html>
5th-and finally The sheet formListarPrestamo.jsp, which is where the error occurs.
<%@page import="java.util.List"%>
<%@page import="Entidades.Prestamo"%>
<%@page import="Entidades.DAO"%>
<%@page import="Entidades.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="prestamo" class="Entidades.Prestamo" />
<jsp:setProperty name="prestamo" property="*"/>
<%
String message = "";//por si hay que mandar un mensaje de error
List<Prestamo> prestamos = null;
DAO dao = new DAO();
try {
prestamos = dao.selectPrestamo();
} catch (Exception ex) {
message = ex.toString();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>formListarPrestamo</title>
<link href="css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="titulo">
<h1>BIBLIOTECA - PRESTAMOS</h1>
<div id="menu">
<div id="opciones">
<h2><a href="index.jsp">Inicio</a></h2>
</div>
<form>
<%if (!message.isEmpty()) {%>
<input name="status_initial" type="text" style="width:400px" value="<%=message%>"/>
<%} else { %>
<br/><br/>
<table border="1">
<tr>
<th>ID</th>
<th>DNI Usuario</th>
<th>ISBN Obra</th>
<th>FechaSolicitud</th>
<th>FechaDevolucion</th>
</tr>
<!--creación de una tabla para mostrar todos los objetos prestamo-->
<%for (int i = 0; i < prestamos.size(); i++) {%>
<tr>
<td><%=prestamos.get(i).getId()%></td>
<td><%=prestamos.get(i).getDNIusuario()%></td>
<td><%=prestamos.get(i).getISBNobra()%></td>
<td><%=prestamos.get(i).getFechasolicitud()%></td>
<td><%=prestamos.get(i).getFechadevolucion()%></td>
</tr>
<%}%>
</table>
<%}%>
</form>
</div>
</div>
</body>
</html>
[1]: https://isstatic.aoverflow.com/lUK8q.png
[2]: https://isstatic.aoverflow.com/wgfLk.png
[3]: https://isstatic.aoverflow.com/SPJhP.png
I don't know if you have to put the class attributes in another way or if the database has something wrong with the request or return date, the loans will in any case have the system dates and their return will be considered late after 10 days pass from that date, but hey, it's a fact that I give you.
Sorry for the length, but I considered that all the parties involved had to be shown (I haven't put the connection, but well, I don't think it's relevant).
Great, I got it to work, I just had to add an empty constructor in the Loan class, which would look like this.
}