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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4253726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:32:40  来源:igfitidea点击:

Extracting data from a database into a text field or text area in Java

javaservletsdatabase-connectivity

提问by Akshay

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?

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

回答by BalusC

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

使用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);
}

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

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

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

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

然而,这为XSS攻击打开了大门。使用 JSTLfn:escapeXml()来防止它。

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

See also:

也可以看看:

回答by Mohamed Shakir

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.

似乎 BalusC 有一个很好的答案,但我想详细说明一下 getItSomehow() 方法。由于您没有指定您使用的数据库,我将举一个例子说明如果您使用的是 mysql,该怎么做。

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

【步骤一】:简单的谷歌搜索会告诉你如何得到正确的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

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

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

        Statement stmnt = con.createStatement();

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

[第3步]:最后你会执行你的select语句

        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:

既然您在 ResultSet 对象中有一组结果,您就可以解析它以获取您请求的数据。您可以使用以下内容:

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

回答by coder25

You can retreive data in JSP in this way:

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

  1. retreive data in bean object
  2. set bean object in request object
  3. dispatch your request to the JSP
  1. 在 bean 对象中检索数据
  2. 在请求对象中设置 bean 对象
  3. 将您的请求发送到 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.

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