在 Java 中将数据库中的数据提取到文本字段或文本区域
我知道我们可以从文本框或文本区域检索文本,然后将数据插入表中。我们怎样才能反其道而行之呢?也就是说,如何根据某种条件将所有数据从数据库放回特定的文本字段或区域?
更新:
我必须做一个小型项目。它是一个人力资源信息系统项目。我必须能够根据员工的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用servlet的
doGet()
方法来预处理请求(您应该在浏览器中调用该servlet的URL)。使用 JSP EL 将其显示在 HTML 输入字段的
value
属性中。然而,这为 XSS 攻击。使用 JSTL
fn:escapeXml()
来阻止它。另请参阅:
Use servlet's
doGet()
method to preprocess the request (you should call the URL of this servlet in browser).Use JSP EL to display it in HTML input field's
value
attribute.However, this sets doors open to XSS attacks. Use JSTL
fn:escapeXml()
to prevent it.See also:
BalusC 似乎对此有一个很好的答案,但我想详细说明一下 getItSomehow() 方法。由于您没有指定您使用的数据库,我将给您一个示例,说明如果您使用 mysql,如何执行此操作。
[第 1 步]:简单的 google 搜索将告诉您如何获取正确的 jdbc
[第 2 步]:现在您将获得到数据库的连接并准备好语句
执行您的 select 语句
[第 3 步]:最后您将立即 您在 ResultSet 对象中有一组结果,您可以解析它以获取您请求的数据。您可以使用以下内容:
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
[Step 2]: Now you will get a connection to the db and get a statement ready
[Step 3]: Finally you will execute your select statement
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:
您可以通过以下方式在 JSP 中检索数据:
遵循以下代码:
您也可以使用表达式语言来避免 scriplet 标记。
You can retreive data in JSP in this way:
Follow this code:
You can use expression language also to avoid scriplet tag.