【问题标题】:add dynamic data inside database using dynamic array JSP使用动态数组 JSP 在数据库中添加动态数据
【发布时间】:2017-05-12 03:46:46
【问题描述】:

我必须在我的数据库中添加数据,从用户动态创建的表包含 2 列是 (ID,Name)

如何检索每个用户插入的行的数据并将它们添加到我的数据库中

注意,我在做一个 JSP 页面,最重要的是获取表单内提交的值

您可以查看以下代码:

    <h1 align="center">Please Select How many countries to add </h1>
    <form action="addCountry.jsp" method="post">
        <input name="nbCountry" placeholder="Number of Countries To Add" class="inputText" style="margin-left: 480px; width:300px;text-align:center" type="text"><br><br>
        <input value="ADD" class="inputText" style="margin-left: 600px;" type="submit" align="middle">
    </form> <br><br><br> 

    <%           String nbCountry = request.getParameter("nbCountry");
        int numCountry = 0;
        if (nbCountry != null && !nbCountry.isEmpty()) {
            numCountry = Integer.parseInt(nbCountry);
        }
        int countryid[] =new int[numCountry];
        String countryName[] = new String[numCountry];
    %> 


    <table border="1" align="center" height="300" width="250" style="text-align:center">
        <tr>
            <td style="text-align:center">Country ID</td>
            <td style="text-align:center">Country Name</td>
        </tr>
        <% while (rst.next()) {%>
        <tr><td><%=rst.getInt(1)%></td><td><%=rst.getString(2)%></td></tr>
        <% }
                for (int i = 0; i < numCountry; i++) {%>
        <tr>
        <form action="addCountry.jsp" method="post">
            <td width="30" height="30"><input type="text" name="<%=countryid[i]%>" style="text-align:center"><%=countryid[i]%></td>
            <td width="30" height="30"><input type="text" name="<%=countryName[i]%>" style="text-align:center"><%=countryName[i]%></td>
        </form>
           </tr> <% }%>
        <tr><td colspan="2"><input type="submit" value="add" style="width: 100%"></td></tr>
    </table>

    <%    stmt.close();
            dbConnection.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    %>

</body>
<%@include file="footer.jsp"%>

【问题讨论】:

    标签: html mysql oracle jsp jdbc


    【解决方案1】:

    您没有提到您面临的错误。而且您提交的代码并不完整。无论如何,我在您的代码中发现了一些问题,纠正这可能会对您有所帮助。 "style="text-align:center"> style="text-align:center">

    我已经从输入标签的名称属性值中删除了双引号,这将设置名称属性的确切值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-21
      • 2014-07-15
      • 2013-06-10
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      相关资源
      最近更新 更多