How to insert check box value to the oracle database using jsp? I want to create hotel's package. where the admin will select the activities to insert in the PACKAGE table. I also have the ACTIVITY table, where it have the PK for every activities.
Here is my code
<% String pack_id=""; Class.forName("oracle.jdbc.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:orcl", "myvgh", "myvgh"); try{ Statement statement=conn.createStatement(); ResultSet rs=statement.executeQuery("SELECT NVL(MAX(PACK_ID),0) + 1 FROM PACKAGE"); while(rs.next()){ pack_id = rs.getString(1); } }catch(Exception e1) {} %> <form> <table width="871" height="413" border="0"> <tr> <tr> <td> </td> <td><input name="pack_id" type="type" value="<%= pack_id %>" size="3" readonly="readonly" /></td> </tr> <td width="124"> <p>Package Name</p></td> <td width="29">:</td> <td width="704"> <label for="pack_type"></label> <input type="text" name="pack_type" id="pack_type" /> </td> </tr> <tr> <td><p>Night and Day</p></td> <td>:</td> <td> <label for="pack_day"></label> <input type="text" name="pack_day" id="pack_day" /> </td> </tr> <tr> <td><p>Meal</p></td> <td>:</td> <td> <label for="pack_meal"></label> <input type="checkbox" name="pack_meal" id="breakfast" />Breakfast <input type="checkbox" name="pack_meal" id="Lunch" />Lunch <input type="checkbox" name="pack_meal" id="Dinner" />Dinner </td> </tr> <tr> <td><label for="activity8"> Activity<br /> </label></td> <td>: </td> <td><input name="activity7" type="checkbox" id="act_name" name="act_name" value="caltural_night" /> Cultural Night <input name="activity" type="checkbox" id="act_name" name="act_name" value="batik_painting" />Batik Painting </td> </tr> <tr> <td> </td> <td> </td> <td><input type="checkbox" id="act_name" name="act_name" value="pounding_rice" />Pounding Rice <input type="checkbox" id="act_name" name="act_name" value="fishing" />Fishing </td> </tr> <tr> <td> </td> <td> </td> <td><input type="checkbox" name="act_name" id="act_name" value="kite_making" /> Kite Making <input type="checkbox" id="act_name" name="act_name" value="gagau_ikan" />"Gagau Ikan"</td> </tr> <tr> <td> </td> <td> </td> <td><input type="checkbox" id="activity12" name="act_name" value="iks_visit" /> IKS Visit <input type="checkbox" id="activity13" name="act_name" value="bbq" />BBQ</td> </tr> <tr> <td>Price (RM)</td> <td>:</td> <td> <label for="pack_price"></label> <input name="pack_price" type="text" id="pack_price" size="12" /> </td> </tr> <tr> <td>Max Person</td> <td>:</td> <td> <label for="pack_max"></label> <input name="pack_max" type="text" id="pack_max" size="7" /> </td> </tr> </table> <div align="center"> <input type="submit" name="submit2" id="submit2" value="Submit" /> <input type="submit" name="clear2" id="clear2" value="Clear" /> </div></td> </tr> </table> </form>
Here is a simple jsp code that insert the selected checkbox values into database.
1)checkbox.jsp:
<html> <body> <form method="post" action="insertmultiple.jsp" > Select Languages:<br> <input type="checkbox" name="lang" value="C/C++">C/C++<br> <input type="checkbox" name="lang" value="JAVA">Java<br> <input type="checkbox" name="lang" value="C#">C#<br> <input type="checkbox" name="lang" value="PERL">PERL<br> <input type="checkbox" name="lang" value="PYTHON">PYTHON<br> <input type="submit" value="Submit"> </form> </body> </html>
2)insertmultiple.jsp:
<%@page import="java.sql.*"%> <% String languages=""; String lang[]=request.getParameterValues("lang"); for(int i=0;i<lang.length;i++){ languages+=lang[i]+" "; } try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); int i=st.executeUpdate("insert into student(languages) values('"+languages+"')"); out.println("Data is successfully inserted into database."); } catch(Exception e){ System.out.println(e);
} %>
Thank you for the answer. I've tried to edit this code. But how to insert another column in the database?
I have this error org.apache.jasper.JasperException: java.lang.NullPointerException
Here is my createPackageProcess.jsp codes
<body> <% String pack_id = request.getParameter("pack_id"); String pack_name = request.getParameter("pack_name"); String pack_day = request.getParameter("pack_day"); String pack_price = request.getParameter("pack_price"); String pack_max = request.getParameter("pack_max"); String pack_act = request.getParameter("pack_act"); String meal=""; String pack_meal[]=request.getParameterValues("pack_meal"); for(int i=0;i<pack_meal.length;i++){ meal+= pack_meal[i]+" "; } String act=""; String act_name[]=request.getParameterValues("act_name"); for(int i=0;i<act_name.length;i++){ act+= act_name[i]+" "; } %> <% String addPackage= "INSERT INTO ACTIVITY VALUES('"+pack_id+"','"+pack_name+"','"+pack_day+"','"+pack_meal+"','"+pack_act+"'," + "'"+pack_price+"','"+pack_max+"')"; Statement statement = conn.createStatement(); statement.executeUpdate(addPackage); %> <script language="javascript"> alert("Successfully Inserted!"); window.location.href = "adminCreatePackage.jsp"; </script> } } </body>
Ads