mardi 31 mai 2016

To insert unchecked value of a check box into database

I need help in inserting the unchecked value of a check box into mysql database using servlet.

Work Flow:

1.From a list of check box values the user checks some of the values and the remaining values are set unchecked

2.After selecting the values the user hits the save button

3.On clicking the save button,the checked values should be stored in one table and the unchecked values should be stored in another table

Issue

I have used the general method to insert the values into database,for me the checked values are getting inserted into one table(pdt_list).In the same way I need to insert the unchecked values into another table say(no_pdt_list). Both the insertion should happen once the save button is clicked.

This is my code

products.jsp

<%@page import="java.util.List"%>
    <%@page import="web.Products"%>
    <%@page import="java.util.ArrayList"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
     <form method="post" action="Save_Products">   
<b>          
            Brand Name:<font color="green">
            <% String brand_name=(String)session.getAttribute("brand_name");
       out.print(brand_name);%> 
        <c:set var="brand_name" value="brand_name" scope="session"  />
       </font></b>         
            <table>            
                <tr>                
                    <th> Products</th> 
                    <th> Description </th>
                </tr>
                <tr>
               <td> <b><%
      List<Products> pdts = (List<Products>) request.getAttribute("list");
      if(pdts!=null){
        for(Products prod: pdts){
           out.println("<input type=\"checkbox\" name=\"prod\" value=\""  + prod.getProductname() + "\">"  + prod.getProductname()+"<br>");
            } %> </b></td>   

            <td><%for(Products prod: pdts){
            out.println("<input type=\"text\" name=\"desc\" style=\"width:50px; height:22px\"/><br/>"); 
        } 

      }  
            %> </td>  

        </tr>
        <br/>
        <br/>
        <tr><td align="center">  <input type="submit" value="Save" name="save"/> </td></tr> 
            </table>
    </form>
    </body>
    </html>

Servlet code

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;


public class Save_Products extends HttpServlet {
 static final String dbURL = "jdbc:mysql://localhost:3306/pdt";
     static final String dbUser = "root";
     static final String dbPass = "root";
    @Override
     public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         response.setContentType("text/html;charset=UTF-8");    
        PrintWriter out = response.getWriter();
            ResultSet rs=null;
            Connection connection = null;  
            try{
      HttpSession session = request.getSession();
    String brand_name =(String) session.getAttribute("brand_name");
String [] prod_list = request.getParameterValues("prod");
String [] desc_list = request.getParameterValues("desc");
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection (dbURL,dbUser,dbPass);

String sql="insert into pdt_list(brand_name,product_name,desc)values(?,?,?)";
PreparedStatement prep = connection.prepareStatement(sql); 
int num_values = Math.min(prod_list.size(), desc_list.size());

int count_updated = 0;
for(int i = 0; i < num_values; i++){
    prep.setString(1, brand_name);
    prep.setString(2, prod_list[i]);
    prep.setString(3,desc_list[i]);
    count_updated += prep.executeUpdate();
}
if(count_updated>0)
{    
   out.print("Products Saved Successfully...");
 RequestDispatcher rd=request.getRequestDispatcher("Save_Success.jsp");    
            rd.forward(request,response);    
}
else{
    RequestDispatcher rd=request.getRequestDispatcher("Save_Failure.jsp");    
            rd.forward(request,response);   
}

prep.close();
       }
     catch(Exception E){
//Any Exceptions will be caught here
System.out.println("The error is"+E.getMessage());

    }  

        finally {
            try {
                connection.close();
            } 
        catch (Exception ex) {
                System.out.println("The error is"+ex.getMessage());
            }
                }

}

}




Aucun commentaire:

Enregistrer un commentaire