lundi 30 novembre 2015

How to insert multiple checkbox data jsp into database using three-tier archi

I want to insert multiple data into the database using checkbox, but I dont know how to insert it using three-tier. I tried to use an array to store it into the database, but it failed.

Below is snippet of my code addTask.jsp

<form action="addTaskAction.jsp">                  
     <h1>New Task</h1>
     <table border="0"> 
       <tr><td>
           <p><label for="taskDesc">Task Description</label>
       </td><td>
            <textarea rows="4" cols="41" name="taskDesc" ></textarea></p>
       </td></tr>
       <tr><td>
            <p><label for="groupId">Labour Groups</label>
       </td><td>
            <input type="checkbox" name="lgroup" value="driver">Driver Group                                        
            <br>
            <input type="checkbox" name="lgroup" value="patching">Road Patching Group                                         
            <br>
            <input type="checkbox" name="lgroup" value="rshoulder">Road Shoulder Group                                           
            <br></p>
     </td></tr>
   </table>
   <p><input type="submit" value="Submit"></p>
</form>

then, below is addTaskAction.jsp

<% 

String taskDesc = request.getParameter("taskDesc");    

String groupId = "";
String arr[] = request.getParameterValues("lgroup");
for(int i=0; i<arr.length; i++){
    groupId += arr[i]+" ";
}

String a = task.addTask(taskId, taskDesc, taskDate, taskDuration, groupId, roadId);

        if((a.equals("success"))){
        %><script>alert("Task Successfully Added ");</script>
        <script>window.location="viewTask.jsp";</script><%
        }
        else{%>
        <script>alert("Adding Task Failed ");</script>
        <script>window.location="addTask.jsp";</script><%
        }
        %>

Then, this is task.java (Controller)

public static String addTask(String taskDesc, String groupId){
    String a = taskDA.addTask(taskDesc, groupId);
    return a;
}

Then this is taskDA.java (Data Access Layer)

public static String addTask(String taskDesc, String groupId){

    Connection myConn;
    Statement myStmt;

    try {
        myConn = connectionManager.getConnection();
        myStmt = myConn.createStatement();

        ResultSet result = myStmt.executeQuery("insert into TASK (TASKDESC, G_ID) values ('" + taskDesc + "','" + groupId + "') ");

        return "success";

        } catch (Exception sqlEx) {
        System.err.println(sqlEx);
        return "a:" + sqlEx;
    }  
}

I really hope that anyone can help me with this problem, pleaseee. It really means a lot




Aucun commentaire:

Enregistrer un commentaire