Mysql – How to insert multiple check boxes values in single column one value in single row in database using jsp

jspmysqlservlets

I want to store multiple check boxes values in database using JSP in single column of database but single value in single row.
My JSP code QuickFunction.jsp:

    <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2 align="center"> Quick Function</h2>

<form action="QuickServlet">
                <table border="1" align="center">
            <tr><td width=50 align="center">1</td>
            <td>Book A voucher</td>
            <td>Sales Voucher</td>
                <td><input type="checkbox" name="voucher"
                    value="Sales Voucher" /></td>

            </tr>
            <tr><td></td><td></td>
            <td>Purchase Voucher</td>
                <td><input type="checkbox" name="voucher"
                    value="Purchase Voucher" /></td>

            </tr>
            <tr><td></td><td></td>
            <td>Receipt</td>
                <td><input type="checkbox" name="voucher"
                    value="Receipt" /></td>

            </tr>
            <tr><td></td><td></td>
            <td>Payment </td>
                <td><input type="checkbox" name="voucher"
                    value="Payment" /></td>

            </tr>
            <tr>
            <td></td><td></td><td> Contra </td>
                <td><input type="checkbox" name="voucher"
                    value="Contra" /></td>

            </tr>
            <tr><td></td><td></td>
            <td> Journal </td>
                <td><input type="checkbox" name="voucher"
                    value="Journal"></td>

            </tr>
       </table>
        <input type="submit" value="Submit"/>
     </form>
     </body>
    </html>

My servlet code QuickServlet (doGet method):

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        String voucher = "";
        String a[]=request.getParameterValues("voucher");
        for(int i=0;i<a.length;i++){
            voucher+=a[i]+"";
        }

        DBConnection dbc=new DBConnection();    
         Connection con=dbc.getNewConnection();

         Statement st = null;
         ResultSet rs = null;
         try 
            {
                st = con.createStatement();
            }
            catch (SQLException e) 
            {
                e.printStackTrace();
            }

            String updatecust = "insert into vouchers(voucher) values('"+voucher+"');";
                    try 
            {

                int i=st.executeUpdate(updatecust);

                if(i>0){
                out.print(" saved successfully....!!");
                }
                else
                   out.print("Sorry..!! Got an exception.");
            }
            catch (SQLException e) 
            {
                e.printStackTrace();
            }

    }

This stores check boxes values in single column in single row as below :

mysql> select * from vouchers;
+--------------------------------------+
| voucher                              |
+--------------------------------------+
| Sales VoucherPurchase VoucherReceipt |
+--------------------------------------+

But i want to store one value in one row and so on. As below(Expected output):

mysql> select * from vouchers;
+------------------+
| voucher          |
+------------------+
| Sales Voucher    |
| Purchase Voucher |
| Receipt          |
+------------------+

Please help me. Thanks in advance!!

Best Solution

In your code, First your concatenating all voucher the using for loop and then inserting.

If you want each voucher is to be inserted in row wise then instead of concatenation voucher in for loop, You call insert query with in for loop and insert voucher.

Like this:

   for(int i=0;i<a.length;i++){
       String updatecust = "insert into vouchers(voucher) values('"+voucher+"');";
    }
Related Question