I have a problem, I have some dates stored in SQL in DATE, but I can't get the dates in day/month/year format (with /), and I can't get the null when there is a date that doesn't exist, it doesn't show me .
I am using MySQL, and the results of the query are displayed in a table.
Let's see if you can tell me how to approach it.
The Dao I do is the following:
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;
}
and this is the jsp sheet
<%@page import="java.util.List"%>
<%@page import="Entidades.Prestamo"%>
<%@page import="DAO.DAOPrestamo"%>
<%@page import="Conexion.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;
DAOPrestamo dao = new DAOPrestamo();
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="../Menu.jsp">Inicio</a></h2>
<h2><a href="V2HojaPrestamos.jsp">Atras</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>Fecha Solicitud</th>
<th>Fecha Devolucion</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>
Initially I see that it is a very basic page in JSP, in fact you do not use any framework, which is not bad, I suppose it is an academic exercise... and I will continue along that line.
Given the structure of your code, I suggest that you implement a utility class with a static method that allows you to convert dates to the desired format and otherwise returns an empty string in those cases where the date is NULL.
To convert dates to the desired format, the proper thing would be to use the SimpleDateFormat class, this allows you to build a pattern under which the date will be produced, I suggest reading the documentation so that you have it a little clearer ( https://docs.oracle .com/javase/7/docs/api/java/text/SimpleDateFormat.html )
In principle the method could be something like this:
With the above, it would be enough to achieve your task simply in the JSP, you should make a couple of changes such as importing the utility class, and on the other hand, use it in the table, something like this:
Now, it all depends on where you want to go, you could do something more generic even adding one more parameter to the utility function, something like this:
With this method you can not only pass the date but also the desired format, so you could reuse this utility on other occasions. This would be its use: