2

我创建了一张桌子。其中的值将从数据库中动态创建。我在最后一列添加了一个复选框。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>
4

1 回答 1

0

One thing, do not write java code in JSP at all. JSPs are meant for representation only. Still as answer to this question you can follow this.

You can set the value of checkbox as rs.getString("id") where id is the primary key of the row in the table.

After you submit the form you can retrieve che checkbox values as

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

and using its cbValues.length, you can know how many records need to be created in the database.

Also instead of writing out.println() statements, you should write.

<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>

Well this is not also the good way because it breaks the purpose of MVC architecture.Just for current situation, do this.

Next time follow the MVC pattern and do not write java code in jsp at all. Its a very bad thing.

于 2013-07-30T08:36:21.197 回答