Insert update and delete in servlet and jsp -
i new servlet , jsp trying achieve operation view,add , update , delete dont have idea achieve operation how forward page , maintain session , pass value jsp kindly in right way. had gone many tutorial no idea , here tried
controller.java
package servlet; import java.io.ioexception; import java.io.printwriter; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import db.dbconnect; public class controller extends httpservlet { private static final long serialversionuid = 1l; public controller() { super(); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); printwriter out=response.getwriter(); string nam = request.getparameter("name"); string pass = request.getparameter("pass"); string email = request.getparameter("emailid"); string phono = request.getparameter("phone"); } public void view() throws classnotfoundexception { try { string sql="select * reg"; class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/mine", "root", "android"); statement stmt=con.createstatement(); resultset rs=stmt.executequery(sql); while(rs.next()) { rs.getstring("name"); rs.getstring("pass"); rs.getstring("emailid"); rs.getstring("phoneno"); system.out.println(rs.getstring("name")); } } catch (sqlexception e) { e.printstacktrace(); } return; } public string insertuser() throws sqlexception { string sql = "insert reg(name,pass,emailid,phoneno) values(?,?,?,?)"; connection con = null; preparedstatement prep = null; try { class.forname("com.mysql.jdbc.driver"); con = drivermanager.getconnection("jdbc:mysql://localhost:3306/mine", "root", "android"); prep = con.preparestatement(sql); prep.setstring(1, "nam"); prep.setstring(2, "pass"); prep.setstring(3, "email"); prep.setstring(4, "phono"); prep.executeupdate(); prep.close(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return "successfully added"; } public string update() throws sqlexception { string sql = "update reg set pass=?,emailid=?,phoneno=? name=?"; connection con = null; preparedstatement prep = null; try { class.forname("com.mysql.jdbc.driver"); con = drivermanager.getconnection("jdbc:mysql://localhost:3306/mine", "root", "android"); prep = con.preparestatement(sql); prep.setstring(1, "nam"); prep.setstring(2, "pass"); prep.setstring(3, "email"); prep.setstring(4, "phono"); prep.executeupdate(); prep.close(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return "successfully update"; } public string delete() throws sqlexception { string sql = "delete form reg name=?"; connection con = null; preparedstatement prep = null; try { class.forname("com.mysql.jdbc.driver"); con = drivermanager.getconnection("jdbc:mysql://localhost:3306/mine", "root", "android"); prep = con.preparestatement(sql); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return "successfully delete"; } }
add.jsp::
<form action="/controller" method="post"> user name:<input type="text" name="name" > password:<input type="password" name="pass"> email-id<input type="text" name="emailid"> phone number:<input type="text" name="phone"> <input type="submit" value="submit"> </form>
update.jsp:
<form action="/controller" method="post"> user name:<input type="text" name="name" > password:<input type="password" name="pass"> email-id<input type="text" name="emailid"> phone number:<input type="text" name="phone"> <input type="submit" value="update"/> </form>
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5"> <display-name>vweb2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>controller</display-name> <servlet-name>controller</servlet-name> <servlet-class>servlet.controller</servlet-class> </servlet> <servlet-mapping> <servlet-name>controller</servlet-name> <url-pattern>/controller</url-pattern> </servlet-mapping> </web-app>
view.jsp
, delete.jsp
2 page dono how retrieve information servlet jsp
i think better form eg. http://danielniko.wordpress.com/2012/04/17/simple-crud-using-jsp-servlet-and-mysql/
Comments
Post a Comment