【问题标题】:How to dynamically include values in checkbox using JSP?如何使用 JSP 在复选框中动态包含值?
【发布时间】:2012-12-06 09:06:04
【问题描述】:

我创建了一张桌子。其中的值将从数据库中动态创建。我在最后一列添加了一个复选框。 Now what I like to know is that, when the checkbox is selected that particular row alone want to be stored in new database, so according to how much checkbox is selected that much row should be added in the database.谁能帮我解决这个问题?
提前谢谢...

我的代码:

<%@ page import="java.sql.*" %> 
<%
    String connectionURL = "";
    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;
    int f=0;
%>

<html>
    <head>
        <title>csepoff</title>
    </head>

    <body>
        <form name="cse" action="csepoffcheck1.jsp">
        <%
            Class.forName("com.mysql.jdbc.Driver").newInstance ();
            connection = DriverManager.getConnection(connectionURL,"root","tajmahalss");
            statement = connection.createStatement();
            String[] batchcse=request.getParameterValues("batchcse");
            String batchhcse="";
            String data="";
            String data1="";
            String data2="";

            for(int i=0; i<batchcse.length; i++) {
                batchhcse+=batchcse[i]+" ";
            }

            rs=statement.executeQuery("select * from signup where batch='"+batchhcse+"'");

            out.println("<table border='1'><tr>");
            out.println("<th>NAME</th><th>DEPARTMENT</th><th>BATCH</th>");
            out.println("<th>FATHER NAME</th><th>MOTHER NAME</th><th>SELECT</th>");

            while(rs.next()){
                out.println("<tr>");
                out.println("<td>"+rs.getString("name")+"</td>");
                out.println("<td>"+rs.getString("depart")+"</td>");
                out.println("<td>"+rs.getString("batch")+"</td>");
                out.println("<td>"+rs.getString("fname")+"</td>");
                    out.println("<td>"+rs.getString("mname")+"</td>");
                out.println("<td><input type=\"checkbox\" name=\"checkbox\" value=\"${}\"></td>");
                out.println("</tr>");
            }
            out.println("</table>");
        %>
            <input type="submit" value="SUBMIT">
        </form>
    </body>
</html>

【问题讨论】:

    标签: jsp jdbc


    【解决方案1】:

    有一件事,根本不要在JSP 中编写java 代码。 JSP 仅用于表示。 仍然可以作为这个问题的答案。

    您可以将复选框的值设置为rs.getString("id"),其中id 是表中行的主键。

    提交表单后,您可以将复选框值检索为

    String[] cbValues =  request.getParameterValues("checkBoxNameAsInHtmlForm");
    

    并使用它的cbValues.length,您可以知道需要在数据库中创建多少条记录。

    另外,你应该写而不是写out.println() 语句。

    <form name="cse" action="csepoffcheck1.jsp">
            <table border='1'>
                <th>NAME</th>
                <th>Select</th>
                <%
                    while (rs.next()) {
                %>
                <td><%=rs.getString("name")%></td>
                <td><input type="checkbox" name="checkbox"
                    value="<%=rs.getString("id")%>"></td>
    
                <%
                    }
                %>
            </table>
    
        </form>
    

    这也不是好方法,因为它破坏了 MVC 架构的目的。就目前的情况,这样做。

    下次按照MVC模式,完全不要用jsp写java代码。这是一件非常糟糕的事情。

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 2011-12-30
      • 1970-01-01
      • 2021-09-24
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多