<html> <head> <title>your info mail</title> </head> <body bgcolor=#FAEBD7> <center><u><font color=#1f2e3c>your info mail</font></u></center> <form action="/Insertinfo" method="post"> <table align=center border=0 cellspacing=0 cellpadding=10 width=100 height=0 bgcolor=#faebd7 <tr> <td>Name</td> <td><input type=text name=ID size=20></td> </tr> <tr> <td>password</td> <td><input type="password" name=IDno size=10></td> </tr> <tr> <td>Address</td> <td><input type=text name=ID size=30></td> </tr> <tr> <td>tel no</td> <td><input type=text name=telno size=20></td> </tr> <tr> <td>sex</td> <td><input type=radio name=s value="MALE"> <input type=radio name=s value="FEMALE"></td> </tr> <TR> <td>country</td> <td><select name="Country Name"> <OPTION VALUE ="I">India</OPTION> <OPTION VALUE ="C">Canada</OPTION> <OPTION VALUE ="f">france</OPTION> <OPTION VALUE ="a">America</OPTION> </select> </td> </table> <br> <br> <center> <input type=submit name=submit value=submit> <input type=reset name=reset value=clear> </center> </form> </body> </html>
I have this information in html please let know how to insert information into database using jsp
hi shwetappatil,
use the names like
String str1=request.getParameter("ID");
here id refers to the name of the input field and know you are storing it in str1.
like this store all the field into some strings and insert into database but the field names should be different while giving. You had used same name for name and address.check that and continue.
Hi Friend,
Try the following code:
1)form.jsp:
<html> <head> <title>your info mail</title> </head> <body bgcolor=#FAEBD7> <h1><center><u><font color=#1f2e3c>your info mail</font></u></h1></center></p> <form action="../InsertServlet" method="post"> <table align=center border=0 cellspacing=0 cellpadding=10 width=100 height=0 bgcolor=#faebd7> <tr> <td>Name</td> <td><input type=text name="name" size=20></td> </tr> <tr> <td>password</td> <td><input type="password" name="pass" size=10></td> </tr> <tr> <td>Address</td> <td><input type=text name="address" size=30></td> </tr> <tr> <td>Tel no</td> <td><input type=text name="no" size=20></td> </tr> <tr> <td>Sex</td> <td><input type=radio name="gender" value="MALE">M <input type=radio name="gender" value="FEMALE">F</td> </tr> <TR> <td>Country</td> <td><select name="country"> <OPTION VALUE ="India">India</OPTION> <OPTION VALUE ="Canada">Canada</OPTION> <OPTION VALUE ="France">France</OPTION> <OPTION VALUE ="America">America</OPTION> </select> </td> </table> <br> <br> <center> <input type=submit name=submit value=submit> <input type=reset name=reset value=clear> </center> </form> <p></body> </html>
2)InsertServlet.java:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class InsertServlet extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); String name=req.getParameter("name"); String pass=req.getParameter("pass"); String address=req.getParameter("address"); int no=Integer.parseInt(req.getParameter("no")); String gender=req.getParameter("gender"); String country=req.getParameter("country"); 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 mail(name,password,address,telno,gender,country) values('"+name+"','"+pass+"','"+address+"',"+no+",'"+gender+"','"+country+"')"); out.println("Data is inserted successfully"); con.close(); } catch(Exception e){ System.out.println(e); } } }
Thanks
Ads