java 将数据库中的数据显示到文本框中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2667861/
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-29 22:17:00  来源:igfitidea点击:

displaying data from database in to text box

javajspservlets

提问by srinayak

I have 2 JSP pages as below:

我有 2 个 JSP 页面,如下所示:

projectcategory.jsp

项目类别.jsp

 <%
    Connection con = DbConnect.connect();
    Statement s = con.createStatement();

    ResultSet rs = s.executeQuery("select * from projectcategory");
 %>
        <DIV class="TabbedPanelsContent" align="center">
        <TABLE border="1">
            <TR>
                <TH>CATEGORY ID</TH>
                <TH>CATEGORY NAME</TH>
                <TH>Edit/Update</TH>
            </TR>

            <%
                while (rs.next()) {
            %>
            <%String p=rs.getString(1);%>

            <TR>
                <TD><%=rs.getString(1)%></TD>
                <TD><%=rs.getString(2)%></TD>
                <TD>

                <FORM action="EditPcat.jsp?pcatid=p"><INPUT type="submit"
                    value='edit/update'></INPUT>
                </FORM>

                </TD>
            </TR>

            <%
                }
            %>
        </TABLE>
        </DIV>

another is Editpcat.jsp:

另一个是 Editpcat.jsp:

</head>
<body>

<%String s=request.getParameter("p"); %>



<form action="ProjCatServlet" method="post">
<div align="right"><a href="projectcategory.jsp">view</a></div>
<fieldset>

<legend>Edit category</legend>
<table cellspacing="2" cellpadding="2" border="0">
    <tr>
        <td align="left">Category Id</td>

        <td><input type="text" name="pcatid" value="<%=s%>" ></td>
    </tr>

    <tr>
        <td align="right">Category Name</td>
        <td><input type="text" name="pcatname"></td>
    </tr>

    <tr>
        <td><input type="submit" value="submit"></td>
    </tr>

</table>

<input type="hidden" name="FUNCTION_ID" value="UPDATE">

</fieldset>
</form>

How to display value from one JSP page which we get from database in to text box of another JSP?

如何将我们从数据库中获取的一个 JSP 页面中的值显示到另一个 JSP 的文本框中?

回答by BalusC

You're passing it as a request parameter with the name pcatid:

您将其作为名称为的请求参数传递pcatid

<FORM action="EditPcat.jsp?pcatid=p">

But you're trying to obtain it as a request parameter with the name p:

但是您试图将其作为名称为的请求参数获取p

<%String s=request.getParameter("p"); %>

Align the names out.

将名称对齐。

That said, you tagged this question with servlets, but you aren't using servlets at all. This code is honestly said a disaster. Scriptletsare a poor practice and the JDBC code is leaking resources. Raw Java code belongs in Java classes, not in JSP files. Use taglibs like JSTL to control the page flow and use EL (Expression Language) to access backend data. Go through those tutorialsto get it right.

也就是说,您用 标记了这个问题servlets,但您根本没有使用 servlet。这段代码老实说是一场灾难。Scriptlet是一种糟糕的做法,而且 JDBC 代码会泄漏资源。原始 Java 代码属于 Java 类,而不属于 JSP 文件。使用 JSTL 之类的 taglibs 来控制页面流并使用 EL(表达式语言)访问后端数据。阅读这些教程以使其正确。

Kickoff example:

开场示例:

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    List<Project> projects = projectDAO.list();
    request.setAttribute("projects", projects);
    request.getRequestDispatcher("projects.jsp").forward(request, response);
}

and

<table>
    <c:forEach items="${projects}" var="project">
        <tr>
            <td>${project.id}</td>
            <td>${project.name}</td>
        </tr>
    </c:forEach>
</table>

回答by Maurice Perry

This is very dirty but you can add hidden INPUT tags in your form:

这很脏,但您可以在表单中添加隐藏的 INPUT 标签:

<INPUT type="hidden" name="p1" value="<%=rs.getString(1)%>">
<INPUT type="hidden" name="p2" value="<%=rs.getString(2)%>">

BTW: you should close your statement when no longer needed, otherwise you may use too much resources on the server.

BTW:你应该在不再需要的时候关闭你的语句,否则你可能会使用服务器上的太多资源。