显示给用户后更新数据库记录

发布于 2024-12-25 11:08:09 字数 1212 浏览 0 评论 0原文

我有一个关于如何使用 JSTL 和 java servlet 更新数据库中的特定记录的问题。

好的,调用一条记录,我使用 servlet 来运行查询,以使用这种类型的格式填充 jsp 页面。

        ResultSet results = getRecords.executeQuery();  //run database query
        Result result = ResultSupport.toResult(results);  
        request.setAttribute("result", result);
        RequestDispatcher rd = request.getRequestDispatcher("/showReport.jsp");  //redirect to this page with query info
        rd.forward(request, response);

在jsp页面中,我使用这种格式来填充页面。
行号对应于查询运行方式的值的索引。

    <c:forEach var="row" items="${result.rowsByIndex}"> 
<form action = "/runthis/servlet?id=${row[0]}" method = "get" >
    Employee Name<input type="text"  value="<c:out value="${row[2]}"/>" />
    Department<select>
    <option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option></select>
<input type="submit" value="Save">
</form>
    </c:forEach>

例如,如果有人更改了员工姓名,但保留了部门字段,反之亦然,我该怎么办?

我认为我应该能够将此表单操作踢到“更新 servlet”,但如果不需要,我宁愿不“更新”记录中的每个元素。

另外,我还在学习网络开发,所以如果你能想出更好的方法来预填充东西,通过 javascript 或其他什么,我也愿意尝试,如果这是一个可能的解决方案。

感谢您的帮助!

I have a question on how to update a particular record in a database using JSTL and java servlets.

Ok to call one record, I use a servlet to run a query to populate a jsp page using this type of format.

        ResultSet results = getRecords.executeQuery();  //run database query
        Result result = ResultSupport.toResult(results);  
        request.setAttribute("result", result);
        RequestDispatcher rd = request.getRequestDispatcher("/showReport.jsp");  //redirect to this page with query info
        rd.forward(request, response);

In the jsp page, I use this format to populate the page.
the row number is the corresponding to the index of the value on how the query is run.

    <c:forEach var="row" items="${result.rowsByIndex}"> 
<form action = "/runthis/servlet?id=${row[0]}" method = "get" >
    Employee Name<input type="text"  value="<c:out value="${row[2]}"/>" />
    Department<select>
    <option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option></select>
<input type="submit" value="Save">
</form>
    </c:forEach>

So for instance, if someone changes the Employee Name, but leaves the Department field alone, or vice versa, how would I go about this?

I think I should be able to kick this form action to a "update servlet", but I would rather not "update" every element in the record if I do not need to.

Also I'm still learning web dev, so if you can think of a better way to prepopulate stuff, via javascript or whatever, I'm willing to try that as well, if it's a possible solution.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2025-01-01 11:08:09

我认为你需要做这样的事情:

<c:forEach var="row" items="${result.rowsByIndex}"> 
    <form action="/runthis/servlet" method="post">
        <input type="hidden" name="id" value="<c:out value="${row[1]}"/>" />
        <input type="hidden" name="EmployeeName_orig" value="<c:out value="${row[2]}"/>" />
        Employee Name<input type="text" name="EmployeeName" value="<c:out value="${row[2]}"/>" />
        <input type="hidden" name="Department_orig" value="<c:out value="${row[4]}"/>" />
        Department
        <select id="Department" name="Department">
            <option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option>
        </select>
        <input type="submit" value="Save" />
    </form>
</c:forEach>

然后,在你的更新servlet(这里称为/runthis/servlet)中,你做某种形式的更新员工并设置行ID的名称和部门。请注意,我在隐藏字段中发送 id,但也可以像您在上面的讨论中提到的那样在 url 中发送它!

关于更新记录中每个元素的评论:

这可以通过不同的方式完成。一种方法是首先从数据库加载记录,然后比较字段以了解要更新哪些字段。但这需要在更新之前从数据库中进行额外的读取。

另一种方法(我在建议的解决方案中准备的方法)是具有隐藏字段,表单中的每个输入都有一个隐藏字段。该隐藏字段保存其输入对应项的原始值。这样您就可以将原始值与新值进行比较,而无需从数据库中读取。另一方面,您现在为每个发布请求通过线路传输双倍的数据量。

i think you need to do something like this:

<c:forEach var="row" items="${result.rowsByIndex}"> 
    <form action="/runthis/servlet" method="post">
        <input type="hidden" name="id" value="<c:out value="${row[1]}"/>" />
        <input type="hidden" name="EmployeeName_orig" value="<c:out value="${row[2]}"/>" />
        Employee Name<input type="text" name="EmployeeName" value="<c:out value="${row[2]}"/>" />
        <input type="hidden" name="Department_orig" value="<c:out value="${row[4]}"/>" />
        Department
        <select id="Department" name="Department">
            <option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option>
        </select>
        <input type="submit" value="Save" />
    </form>
</c:forEach>

and then, in your update servlet (here called /runthis/servlet) you do some form of update employee and set the name and department where the row is id. note that i send the id in a hidden field, but it could just as well be sent in the url as you mentioned in our discussion above!

about your remark on updating every element in a record:

this can be done in different ways. one way is to load the record from database first, and compare the fields to know what fields to update. but that would require an extra read from the database before the update.

another way (the one i have prepared for in my proposed solution) is to have hidden fields, one for each input in your form. this hidden field holds the original value for its input counterpart. this way you can compare the original value with the new value without having to do a read from the database. on the other hand, you're now transferring the double amount of data over the line for each post request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文