hi i want to insert dropdown values into a database table by using jsp.
1)form.jsp:
<html> <body> <form method = "post" action = "/Examples/GetParameterValues"> <p>Which of the programming languages you like the most</p> <select name="lang"> <option value="C/C++">C/C++</option> <option value="Java">C/C++</option> <option value=".NET">C/C++</option> <option value="Perl">C/C++</option> <option value="Pascal">C/C++</option> </select> </form> </body> </html>
2)GetParameterValues
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class GetParameterValues extends HttpServlet{ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String languages=" "; response.setContentType("text/html"); PrintWriter pw = response.getWriter(); String[] lang = request.getParameterValues("lang"); for(int i=0; i<lang.length; i++){ lanuages+=lang[i]+" "; } try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" ); Statement st=conn.createStatement(); st.executeUpdate("insert into data(languages) values('"+languages+"')"); pw.println("Inserted Successfully!"); } catch(Exception e){} } }
1)form.jsp:
<html> <body> <form method = "post" action = "/Examples/GetParameterValues"> <p>Which of the programming languages you like the most</p> <select name="lang" multiple> <option value="C/C++">C/C++</option> <option value="Java">C/C++</option> <option value=".NET">C/C++</option> <option value="Perl">C/C++</option> <option value="Pascal">C/C++</option> </select> </form> </body> </html>
2)GetParameterValues
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class GetParameterValues extends HttpServlet{ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String languages=" "; response.setContentType("text/html"); PrintWriter pw = response.getWriter(); String[] lang = request.getParameterValues("lang"); for(int i=0; i<lang.length; i++){ lanuages+=lang[i]+" "; } try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" ); Statement st=conn.createStatement(); st.executeUpdate("insert into data(languages) values('"+languages+"')"); pw.println("Inserted Successfully!"); } catch(Exception e){} } }
1)form.jsp:
<html> <body> <form method = "post" action = "insert.jsp"> <p>Which of the programming languages you like the most</p> <select name="lang" multiple> <option value="C/C++">C/C++</option> <option value="Java">C/C++</option> <option value=".NET">C/C++</option> <option value="Perl">C/C++</option> <option value="Pascal">C/C++</option> </select> </form> </body> </html>
2)insert.jsp
<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%
String languages=" ";
String[] lang = request.getParameterValues("lang");
for(int i=0; i
1)form.jsp:
<html> <body> <form method = "post" action = "insert.jsp"> <p>Which of the programming languages you like the most</p> <select name="lang" multiple> <option value="C/C++">C/C++</option> <option value="Java">C/C++</option> <option value=".NET">C/C++</option> <option value="Perl">C/C++</option> <option value="Pascal">C/C++</option> </select> </form> </body> </html>
2)insert.jsp
<%@page import="java.io.*"%> <%@page import="java.sql.*"%> <% String languages=" "; String[] lang = request.getParameterValues("lang"); for(int i=0; i<lang.length; i++){ lanuages+=lang[i]+" "; } try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" ); Statement st=conn.createStatement(); st.executeUpdate("insert into data(languages) values('"+languages+"')"); out.println("Inserted Successfully!"); } catch(Exception e){} %>
Ads