To insert multiple check box values into separate rows in mysql

sound

I need help in inserting the multiple check box values into separate rows in mysql database.

Work flow:

1.The user selects the product check box and enters a description about the product in the text box next to the particular check box.

2.After selecting the check boxes the user hits the save button.

3.On clicking save ,the product details should be inserted into the database. Note: User can select multiple check box values.

Issue:

I have used the general method to insert values,but only one check box value is getting inserted into the database.I need to insert multiple check box values along with its text box values into the database as separate rows once I click on the save button.

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=request.getParameter("prod");
    String desc=request.getParameter("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); 

 prep.setString(1, brand_name);
 prep.setString(2, prod);
 prep.setString(3,desc);

int i=prep.executeUpdate();

if(i>0)
{    
   out.print("Product Details 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());
            }
                }

}

}
imran arshad

In your servlet use request.getParameterValues("prod") to get the array of all the inputs you received. request.getParameter("prod") will just return you one. Same goes for your desc values.

Then loop through the values and insert them all into your DB

String [] prod_list = request.getParameterValues("prod");
String [] desc_list = request.getParameterValues("desc");
// set up your query

PreparedStatement prep = connection.prepareStatement(sql);

// just a precaution here getting min so we do not get outofbounds exception on one of the arrays
// both arrays should be of same size - if not, something is wrong on your jsp logic
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();
}

// Rest of your logic

This is a simpler way of using PreparedStatement. Check how to do batch inserts with PreparedStatement. This should get you started.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

To insert multiple check box values into separate rows in mysql

From Dev

how to insert multiple text box with file uploader values in mysql database?

From Dev

Mysql Insert Multiple Rows with comma separated values with sql statement alone

From Dev

How to insert values from JSON data with multiple objects into rows in MySQL?

From Dev

PHP : insert multiple check boxes values into one MySQL column

From Dev

Insert multiple rows in MySQL and check for random string without long delay (~80 rows each minute)

From Dev

How to insert two different check box values into different columns of one mysql table using php?

From Dev

mysql insert with select query to insert multiple rows

From Dev

Loop through rows and INSERT multiple rows into MySQL

From Dev

MySQL check if multiple rows exist

From Dev

MySQL - INSERT multiple values conditionally

From Dev

MySQL - INSERT multiple values conditionally

From Dev

How to get multiple selected check box values

From Dev

insert multiple rows along with static values

From Dev

insert multiple rows with specific calculated values

From Dev

insert multiple rows along with static values

From Dev

MySQL, insert values in all rows except id

From Dev

How to insert multiple jquery on created rows values into database using php and mysql?

From Dev

Insert multiple rows into a MySQL database from a table

From Dev

UPDATE AND INSERT multiple rows into MySQL database

From Dev

PHP MySQL insert/update multiple rows at once

From Dev

How to insert multiple records/rows in MySQL

From Dev

insert multiple rows via a php array into mysql

From Dev

MySQL INSERT INTO from PHP://INPUT multiple rows

From Dev

insert multiple rows mysql from another table

From Dev

Insert multiple rows without duplicating them - MySQL

From Dev

Insert multiple rows by one mysql query

From Dev

Mysql check if not exist before multiple insert

From Dev

Multiple not exists in MYSQL to check count of rows

Related Related

  1. 1

    To insert multiple check box values into separate rows in mysql

  2. 2

    how to insert multiple text box with file uploader values in mysql database?

  3. 3

    Mysql Insert Multiple Rows with comma separated values with sql statement alone

  4. 4

    How to insert values from JSON data with multiple objects into rows in MySQL?

  5. 5

    PHP : insert multiple check boxes values into one MySQL column

  6. 6

    Insert multiple rows in MySQL and check for random string without long delay (~80 rows each minute)

  7. 7

    How to insert two different check box values into different columns of one mysql table using php?

  8. 8

    mysql insert with select query to insert multiple rows

  9. 9

    Loop through rows and INSERT multiple rows into MySQL

  10. 10

    MySQL check if multiple rows exist

  11. 11

    MySQL - INSERT multiple values conditionally

  12. 12

    MySQL - INSERT multiple values conditionally

  13. 13

    How to get multiple selected check box values

  14. 14

    insert multiple rows along with static values

  15. 15

    insert multiple rows with specific calculated values

  16. 16

    insert multiple rows along with static values

  17. 17

    MySQL, insert values in all rows except id

  18. 18

    How to insert multiple jquery on created rows values into database using php and mysql?

  19. 19

    Insert multiple rows into a MySQL database from a table

  20. 20

    UPDATE AND INSERT multiple rows into MySQL database

  21. 21

    PHP MySQL insert/update multiple rows at once

  22. 22

    How to insert multiple records/rows in MySQL

  23. 23

    insert multiple rows via a php array into mysql

  24. 24

    MySQL INSERT INTO from PHP://INPUT multiple rows

  25. 25

    insert multiple rows mysql from another table

  26. 26

    Insert multiple rows without duplicating them - MySQL

  27. 27

    Insert multiple rows by one mysql query

  28. 28

    Mysql check if not exist before multiple insert

  29. 29

    Multiple not exists in MYSQL to check count of rows

HotTag

Archive