|
|
| inserting value |
Expert:kushal BANSAL
<p>sir i want to create a web application when we use the jsp inserting the value in the jsp after that i want these value inserted the database mySQl with the help of servlet. Inserting form data </p> |
| Answers |
Inserting form data into database using JSP and Servlet<br /><br />Given code will illustrate you, how one can get data into database using JSP and Servlet<br /><br />Example Code...<br /><br />insertData.jsp<br /><br /><html><br /><head><br /><title>Insert value in database</title><br /></head><br /><br /><body><br /><br /><table border="1" width="50%" cellspacing="0" cellpadding="0"><br /> <tr><br /> <td width="100%"><br /> <h1><center>Insert value in Database</center></h1><br /> <form method="POST" action="InsertAction.jsp"><br /> <br /> <table border="1" width="100%" cellspacing="0" cellpadding="0"><br /> <tr><br /> <td width="50%" valign="right"><b>Name:</b></td><br /> <td width="50%"><input type="text" name="username" size="30"></td><br /> </tr><br /> <tr><br /> <td width="50%" valign="right"><b>Job-Position:</b></td><br /> <td width="50%"><input type="text" name="jobposition" size="30"></td><br /> </tr><br /> <tr><br /> <td width="50%" valign="right">&nbsp;</td><br /> <td width="50%" valign="right">&nbsp;</td><br /> </tr><br /> </table><br /> <p><input type="submit" value="Submit" name="B1"><br /> <input type="reset" value="Reset" name="B2"></p><br /> </form><br /> </td><br /> </tr><br /></table><br /></body><br /></html><br />- - - - - - - <br />InsertAction.jsp<br /><br /><%@ page language="java" import="java.sql.*" %><br /><%<br /><br /><br />Connection con = null;<br /> String url = "jdbc:<a href="mysql://localhost:3306/"" target="_blank">mysql://localhost:3306/"</a>;;<br /> String db = "register";<br /> String driver = "com.mysql.jdbc.Driver";<br /> try{<br /> Class.forName(driver);<br /> con = DriverManager.getConnection(url+db,"root","root");<br /> try{<br /> Statement st = con.createStatement();<br /> String username=request.getParameter("username");<br /> String jobposition=request.getParameter("jobposition");<br /> int val = st.executeUpdate("insert user_details values('"+username+"','"+jobposition+"')");<br /> con.close();<br /> out.println("success");<br /> }<br /> catch (SQLException s){<br /> System.out.println("SQL statement is not executed!");<br /> }<br /> }<br /> catch (Exception e){<br /> e.printStackTrace();<br /> }<br /><br />%><br /><br />This will help you to create a small application that will insert the HTML form data into database using JSP and Servlet.<br /><br />
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|