在 Java 中将数据库中的数据提取到文本字段或文本区域

发布于 2024-10-04 15:04:37 字数 467 浏览 0 评论 0原文

我知道我们可以从文本框或文本区域检索文本,然后将数据插入表中。我们怎样才能反其道而行之呢?也就是说,如何根据某种条件将所有数据从数据库放回特定的文本字段或区域?


更新

我必须做一个小型项目。它是一个人力资源信息系统项目。我必须能够根据员工的 ID 更新其详细信息。到目前为止,我设计和设想的方式如下: 有一个 ID 下拉列表。我选择一个并使用表单处理程序单击“确定”。然后它转发到一个 servlet,该 servlet 显示我在添加员工时制作的表单。只是,这些文本字段不是空白,而是由我在添加上述员工时插入的数据组成。那么,现在,如何提取这些列值并将其放回到文本字段中。我尝试过将字段值设置为列属性名称,但显示的只是名称,而不是值。例如,当我设置 value=firstname (如我的数据库中指定的)时,文本字段中的数据是“firstname”,而不是员工的实际名字。也许我的处理方式是错误的。有人可以确切地告诉我如何检索这些值吗?

I know that we can retrieve text from a text box or text area and then insert the data into a table. How can we do the opposite? That is, how to place all the data back into specific text fields or areas from a database based on some condition?


Update:

I have to do a mini-project. Its a HR Information System project. I must be able to update the details of an employee based on his ID. The way I have designed and envisioned it so far is as follows: There is a dropdown list of IDs. I select one and click OK using a form handler. It then forwards to a servlet that displays the form that I had made while adding an employee. Only, instead of being blank, these text fields consist of the data that I had inserted when adding the abovementioned employee. So, now, how do I extract these column values and put it back into text fields. I have experimented with setting the field values as column attribute names, but all that gets displayed is the name, and not the value. For example, when I set value=firstname (as specified in my database), the data in the textfield is "firstname" and not what the employee's first name actually is. Maybe I'm going about it the wrong way. Can someone please tell me exactly how to retrieve these values?

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

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

发布评论

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

评论(3

天生の放荡 2024-10-11 15:04:37

使用servlet的doGet()方法来预处理请求(您应该在浏览器中调用该servlet的URL)。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Employee employee = getItSomehow();
    request.setAttribute("employee", employee);
    request.getRequestDispatcher("/WEB-INF/edit.jsp").forward(request, response);
}

使用 JSP EL 将其显示在 HTML 输入字段的 value 属性中。

<input name="firstname" value="${employee.firstname}" />

然而,这为 XSS 攻击。使用 JSTL fn:escapeXml() 来阻止它。

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...
<input name="firstname" value="${fn:escapeXml(employee.firstname)}" />

另请参阅:

Use servlet's doGet() method to preprocess the request (you should call the URL of this servlet in browser).

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Employee employee = getItSomehow();
    request.setAttribute("employee", employee);
    request.getRequestDispatcher("/WEB-INF/edit.jsp").forward(request, response);
}

Use JSP EL to display it in HTML input field's value attribute.

<input name="firstname" value="${employee.firstname}" />

However, this sets doors open to XSS attacks. Use JSTL fn:escapeXml() to prevent it.

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...
<input name="firstname" value="${fn:escapeXml(employee.firstname)}" />

See also:

无法言说的痛 2024-10-11 15:04:37

BalusC 似乎对此有一个很好的答案,但我想详细说明一下 getItSomehow() 方法。由于您没有指定您使用的数据库,我将给您一个示例,说明如果您使用 mysql,如何执行此操作。

[第 1 步]:简单的 google 搜索将告诉您如何获取正确的 jdbc

    try{
        Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e){
        throw new AssertionError(e);
    }

[第 2 步]:现在您将获得到数据库的连接并准备好语句

        String url = "jdbc:mysql:///"+database_name;
        Connection con = DriverManager.getConnection(url);

        Statement stmnt = con.createStatement();

执行您的 select 语句

        ResultSet rs = stmnt.executeQuery(
             "SELECT "+ "values you want to retrieve separated by commas"
           +" FROM "+ "table name"
           +" WHERE "+ "condition eg. id=123";

[第 3 步]:最后您将立即 您在 ResultSet 对象中有一组结果,您可以解析它以获取您请求的数据。您可以使用以下内容:

        if(rs.next()){
               employee.setXXXX(rs.getString("XXXX"));
               employee.setYYYY(rs.getString("YYYY"));
         }

It seems like BalusC has a good answer for it but I wanted to elaborate a little bit more on the getItSomehow() method. Since you have not specified what database you are using I will give you an example of how to do it if you were using mysql.

[Step 1]: A simple google search will tell you how to get the correct jdbc

    try{
        Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e){
        throw new AssertionError(e);
    }

[Step 2]: Now you will get a connection to the db and get a statement ready

        String url = "jdbc:mysql:///"+database_name;
        Connection con = DriverManager.getConnection(url);

        Statement stmnt = con.createStatement();

[Step 3]: Finally you will execute your select statement

        ResultSet rs = stmnt.executeQuery(
             "SELECT "+ "values you want to retrieve separated by commas"
           +" FROM "+ "table name"
           +" WHERE "+ "condition eg. id=123";

Now that you have a set of results int he ResultSet object you can parse it to get the data you requested. You could use the following:

        if(rs.next()){
               employee.setXXXX(rs.getString("XXXX"));
               employee.setYYYY(rs.getString("YYYY"));
         }
猫弦 2024-10-11 15:04:37

您可以通过以下方式在 JSP 中检索数据:

  1. 在 bean 对象中检索数据
  2. 在请求对象中设置 bean 对象
  3. 将您的请求分派到 JSP

遵循以下代码:

<%
    forumbean bean=(forumbean)request.getAttribute("userdata");
    String name=bean.getName();
    out.print("<input type='text' id='name' name='name' value='"+name+"'>");
%>

您也可以使用表达式语言来避免 scriplet 标记。

You can retreive data in JSP in this way:

  1. retreive data in bean object
  2. set bean object in request object
  3. dispatch your request to the JSP

Follow this code:

<%
    forumbean bean=(forumbean)request.getAttribute("userdata");
    String name=bean.getName();
    out.print("<input type='text' id='name' name='name' value='"+name+"'>");
%>

You can use expression language also to avoid scriplet tag.

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