【发布时间】:2019-08-07 14:54:36
【问题描述】:
我正在尝试显示我打算更新的用户的数据。
在我的结构中,我输入了一个在查询列表时提取的 dni,然后以另一种形式显示用户的数据。发送此表单后,数据将永久更新。
我的问题是我不知道如何显示我之前搜索过的用户数据。 PE:我正在寻找一个名为 Jhon Doe 且 DNI 为 11111111E 的用户,在第二个表单中,我将有一个包含姓名和姓氏 Jhon Doe 的新表单,现在我在 Mike Doe 表单的这些字段中写入并按下提交。
问题:我不知道如何在我的表单中显示旧名称(我不知道如何显示 Jhon Doe)。
这是我的代码,现在我卡在那个部分,更新它是好的,但名称没有显示)
我已经添加了我的 jsp 网页以查看它们是如何工作的,但无论如何我的问题是关于我设法从将要更新的用户那里显示日期的方式。
我的第一个jsp页面在这里(V2formModificarUsuario1.jsp)
<%@page import="java.util.List"%>
<%@page import="Entidades.Usuario"%>
<%@page import="DAO.DAOUsuario"%>
<%@page import="Conexion.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="usuario" class="Entidades.Usuario" />
<jsp:setProperty name="usuario" property="*"/>
<%
String message = "";
List<Usuario> usuarios = null;
DAOUsuario dao = new DAOUsuario();
try {
//usuarios = dao.selectUsuario("si","nombre");
usuarios = dao.selectAllUsuarios();
} catch (Exception ex) {
message = ex.toString();
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>V2formListarUsuariosAltaNombre</title>
<link href="../css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="titulo">
<h1>BIBLIOTECA - LISTADOS USUARIOS</h1>
<div id="menu">
<div id="opciones">
<h2><a href="../Menu.jsp">Menu inicial</a></h2>
<h2><a href="V2HojaUsuarios.jsp">Atras</a></h2>
</div>
<form id="formulario" action="V2formModificarUsuario2(en obras).jsp" method="POST">
<label for="DNI">Intruduzca DNI del usuario</label><br>
DNI: <input type="text" name="DNI" required="">
<input type="submit" value="buscar">
</form>
<form>
<%if (!message.isEmpty()) {%>
<input name="status_initial" type="text" style="width:400px" value="<%=message%>"/>
<%} else { %>
<br/><br/>
<table border="1">
<tr>
<th>DNI</th>
<th>nombre</th>
<th>apellidos</th>
<th>de alta</th>
</tr>
<%for (int i = 0; i < usuarios.size(); i++) {%>
<tr>
<td><%=usuarios.get(i).getDNI()%></td>
<td><%=usuarios.get(i).getNombre()%></td>
<td><%=usuarios.get(i).getApellidos()%></td>
<td><%=usuarios.get(i).getDeAlta()%></td>
</tr>
<%}%>
</table>
<%}%>
</form>
</div>
</div>
</body>
我的第二个jsp页面在这里(V2formModificarUsuario2.jsp)
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="Entidades.Usuario"%>
<%@page import="DAO.DAOUsuario"%>
<%@page import="Conexion.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="usuario" class="Entidades.Usuario"></jsp:useBean>
<jsp:setProperty name="usuario" property="*"/>
<%
String message = "";
// String nameToChange = "";
String surnameToChange = "";
try {
if ((usuario.getDNI() != null) && (!usuario.getDNI().isEmpty())) {
DAOUsuario dao = new DAOUsuario();
// nameToChange=dao.selectByDNI(request.getParameter("DNI")).getNombre(); comentado por ahora
surnameToChange=dao.selectByDNI(request.getParameter("DNI")).getApellidos();
Usuario usuarios = dao.selectByDNI(usuario.getDNI());
if (usuarios != null) {
if ((usuario.getNombre() != null) && (!usuario.getNombre().isEmpty())
&& (usuario.getApellidos() != null) && (!usuario.getApellidos().isEmpty())) {
dao.update(usuario.getDNI(), usuario.getNombre(), usuario.getApellidos());
message = "User correctly updated.";
} else {
if (request.getParameter("updating") == "1") {
message = "Los campos name, surname and deAlta are required .";
}
}
} else {
message = "user do not exists.";
}
} else {
message = "DNI must not be null.";
}
} catch (Exception ex) {
message = ex.getMessage();
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSPformModificarUsuario2(en obras)</title>
<link href="css/estilos.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="titulo">
<h1>BIBLIOTECA - USUARIO</h1>
<div id="menu">
<div id="opciones">
<h2><a href="index.jsp">Inicio</a></h2>
</div>
<form method="POST" action="V2formModificarUsuario2(en obras).jsp">
<input name="updating" type="hidden" value="1"/>
<%if (!message.isEmpty()) {%>
<input name="message" type="text" style="width:400px" value="<%=message%>"/>
<%} else { %>
<br/><br/>
DNI:
<input name="DNI" type="text" style="width:200px" value="<jsp:getProperty property="DNI" name="usuario"/>" readonly=""/>
<br/><br/>
nombre:
<input name="nombre" type="text" style="width:200px" placeholder="<%=apellidoActualizar%>" value="<jsp:getProperty property="nombre" name="usuario"/>"/>
<br/><br/>
apellidos:
<input name="apellidos" type="text" style="width:200px" value="<jsp:getProperty property="apellidos" name="usuario"/>"/>
<br/><br/>
<input type="submit" value="Actualizar"/>
<%}%>
</form>
</div>
</div>
</body>
最后,这里是我的 DAO。
public Usuario update(String DNI, String nombre, String apellidos) throws Exception {
if ((DNI == null) || (DNI.isEmpty())) {
throw new Exception("DNI must not be null");
}
if ((nombre == null) || (nombre.isEmpty())) {
throw new Exception("name must not be null");
}
if ((apellidos == null) || (apellidos.isEmpty())) {
throw new Exception("surname must not be null");
}
Usuario usuario = selectByDNI(DNI);
if (usuario == null) {
throw new Exception("user do not exist");
}
try (Connection connection = DBConnection.get()) {
if (connection == null) {
throw new Exception("Connection is null");
}
String query = "UPDATE usuario SET nombre=?, apellidos=? WHERE DNI=?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, nombre);
statement.setString(2, apellidos);
statement.setString(3, DNI);
statement.execute();
} catch (Exception ex) {
throw ex;
} finally {
DBConnection.close();
}
usuario = selectByDNI(DNI);
return usuario;
}
public Usuario selectByDNI(String DNI) throws Exception {
if ((DNI == null) || (DNI.isEmpty())) {
return null;
}
Usuario usuario = null;
try (Connection connection = DBConnection.get()) {
if (connection == null) {
throw new Exception("Connection is null");
}
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM usuario WHERE DNI = '" + DNI + "'");
if (rs.next()) {
usuario = new Usuario(rs.getString("DNI"), rs.getString("nombre"),
rs.getString("apellidos"), rs.getString("deAlta"));
}
} catch (Exception ex) {
throw ex;
} finally {
DBConnection.close();
}
return usuario;
}
【问题讨论】: