Вы находитесь на странице: 1из 36

1. Creating insertupdate.jsp page 2.

Creating servlet
a. Right click on source packages.and select new and select servlet

b.

Enter Servlet name and click next

c.

click next and finish

d. Then netbeans creates default structure.

3. Creating beans.
In this post we will learn how to create a package in netbeans and how to create a class in that pakage. Right click on source packages and select package

Enter package name(model in this example) and click next now create a bean class with in this package. for this select that package and right click and select create and select class. and now enter name of that class.In this example that class name is UserBean. and click finish

then netbeans creates a class with default structure. 1. 2. 3. 4. 5. 6. 7. 8. 9. package model; /** * * @author Jagadeesh */ public class UserBean { }

now insert setter methods and getter methods in that been.Simple way to create setter methods and getter methods. 1. Declare variables 2. Select all variables and right click and click on insert code. select setter methods and getter methods and ok

then it will create setter and getter methods. in this example i declared four variables for form values and five variables for display error messages. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. package model; package model; /** * @author Jagadeesh */ public class UserBean { private String userName ; private String dateOfBirth ;

11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71.

private String email ; private String phoneNo ; //for validation error messages private private private private private String userNameError; String dateOfBirthError; String emailError; String phoneNoError; boolean isValid;

//this is for action means edit or submit or delete or update private String action; //construtor.it is invoked when we run jsp page. //so first time no values will display in jsp page public UserBean() { userName =""; dateOfBirth =""; email=""; phoneNo=""; userNameError=""; dateOfBirthError=""; emailError=""; phoneNoError=""; action = "submit"; } public String getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(String dateOfBirth) { this.dateOfBirth = dateOfBirth; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhoneNo() { return phoneNo; } public void setPhoneNo(String phoneNo) { this.phoneNo = phoneNo; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }

72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120.

//setter and getter methods for errors public String getUserNameError() { return userNameError; } public void setUserNameError(String userNameError) { this.userNameError = userNameError; } public String getDateOfBirthError() { return dateOfBirthError; } public void setDateOfBirthError(String dateOfBirthError) { this.dateOfBirthError = dateOfBirthError; } public String getEmailError() { return emailError; } public void setEmailError(String emailError) { this.emailError = emailError; } public String getPhoneNoError() { return phoneNoError; } public void setPhoneNoError(String phoneNoError) { this.phoneNoError = phoneNoError; } public boolean getIsValid() { return isValid; } public void setIsValid(boolean isValid) { this.isValid = isValid; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } }

in next post we will create a class for validate form

4. Creating validation class


Up to now we created insertupdate.jsp ControllerServlet.java package model and bean class now in this post we will create a package and a class crate a package validation and with in this package create a class ValidateForm. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. package validation; /** * * @author Jagadeesh */ public class ValidateForm {

now go to servlet. in that servlet declare variables to get form values and create a method for setting values to bean 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. import import import import import import import import import java.io.IOException; java.io.PrintWriter; javax.servlet.RequestDispatcher; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; model.UserBean; validation.ValidateForm;

/** * * @author Jagadeesh */ public class ControllerServlet extends HttpServlet { //declare values to get form values from jsp page String userName; String dateOfBirth; String email; String phoneNo; String action; UserBean bean = new UserBean(); ValidateForm validateform = new ValidateForm(); protected void processRequest(HttpServletRequest request, HttpServletResponse respo nse) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { //get the values from jsp page userName = request.getParameter("userName"); dateOfBirth = request.getParameter("dateOfBirth");

35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. }

email = request.getParameter("email"); phoneNo = request.getParameter("phoneNo"); action = request.getParameter("action"); if(action.equals("submit")) { //set values to bean.For this call below method setValuesToBean();

} } catch(Exception e) { out.println(e); } finally { out.close(); } } //this method is used to setvalues to bean public void setValuesToBean() { bean.setUserName(userName); bean.setDateOfBirth(dateOfBirth); bean.setEmail(email); bean.setPhoneNo(phoneNo); }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }

public String getServletInfo() { return "Short description"; }

in next post we will learn how to validate form values and how to send errors to jsp page

5. Vaidate form values


in this post we will create methods for validate form values and set errors to bean if those values are invalid. now create methods.
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package validation; import model.UserBean; /** * * @author Jagadeesh */ public class ValidateForm {

UserBean validBean = new UserBean(); public UserBean validateData(UserBean ubean) { //if given username value is not valid then seterror // message and set empty value to input field if(ubean.getUserName().length()==0) { validBean.setUserNameError("please enter valid name"); validBean.setIsValid(false); validBean.setUserName(""); } else if(!ubean.getUserName().matches("[a-zA-Z]*")) { validBean.setUserNameError("please enter valid name"); validBean.setIsValid(false); validBean.setUserName(""); } //if given username value is valid then seterror message to // empty and set value to bean else { validBean.setUserNameError(""); validBean.setIsValid(true); validBean.setUserName(ubean.getUserName()); }

//if given username value is not valid then seterror message // and set empty value to input field if(!ubean.getDateOfBirth().matches("\\d{1,2}-\\d{1,2}-\\d{4}")) { validBean.setDateOfBirthError("please enter valid date"); validBean.setIsValid(false); validBean.setDateOfBirth("");

53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.

} //if given username value is valid then seterror message // to empty and set value to bean else { validBean.setDateOfBirthError(""); validBean.setIsValid(true); validBean.setDateOfBirth(ubean.getDateOfBirth()); }

//if given username value is not valid then seterror message //and set empty value to input field if(!ubean.getEmail().matches("^[\\w-_\\.+]*[\\w_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$")) { validBean.setEmailError("please enter valid email"); validBean.setIsValid(false); validBean.setEmail(""); } //if given username value is valid then seterror message //to empty and set value to bean else { validBean.setEmailError(""); validBean.setIsValid(true); validBean.setEmail(ubean.getEmail()); } //if given username value is not valid then seterror message // and set empty value to input field if(!ubean.getPhoneNo().matches("\\d{10}")) { validBean.setPhoneNoError("please enter valid phoneno"); validBean.setIsValid(false); validBean.setPhoneNo(""); } //if given username value is valid then seterror message to // empty and set value to bean else { validBean.setPhoneNoError(""); validBean.setIsValid(true); validBean.setPhoneNo(ubean.getPhoneNo()); } return validBean; } }

68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100.

6. Calling validate method


in this post we will learn how to call validate class method and how to send values from servlet to that method and how to get errors to servlet and how to display errors in jsp page

view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50.

import import import import import import import import import

java.io.IOException; java.io.PrintWriter; javax.servlet.RequestDispatcher; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; model.UserBean; validation.ValidateForm;

/** * * @author Jagadeesh */ public class ControllerServlet extends HttpServlet { //declare values to get form values from jsp page String userName; String dateOfBirth; String email; String phoneNo; String action; UserBean bean = new UserBean(); ValidateForm validateform = new ValidateForm(); protected void processRequest(HttpServletRequest request, HttpServletResponse respo nse) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { //get the values from jsp page userName = request.getParameter("userName"); dateOfBirth = request.getParameter("dateOfBirth"); email = request.getParameter("email"); phoneNo = request.getParameter("phoneNo"); action = request.getParameter("action"); if(action.equals("submit")) { //set values to bean.For this call below method setValuesToBean(); //check all form values are valid or not. send bean object UserBean checkedbean = validateform.validateData(bean); if(!checkedbean.getIsValid()) { //if data is invalid.set bean object in request and pass that request t o //insertupdate.jsp using forward checkedbean.setAction("submit");

51. 52. ; 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74.

request.setAttribute("error",checkedbean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp") rd.forward(request, response); //now display errors in that jsp page } } } catch(Exception e) { out.println(e); } finally { out.close(); } } //this method is used to setvalues to bean public void setValuesToBean() { bean.setUserName(userName); bean.setDateOfBirth(dateOfBirth); bean.setEmail(email); bean.setPhoneNo(phoneNo); }

in this servlet we validate all values by calling validatedata() and if it is invalid we forward errors to insertupdate.jsp page . we set that bean object in request and we forward page using RequestDispatcher. in next post we will learn how to display errors in jsp page.

7. Displays Error in JSP


now get the attribute from request from servlet and display errors
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55.

<%-Document Author --%>

: index : Jagadeesh

<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="model.UserBean" %> <% UserBean bean; bean = new UserBean(); //get request from servlet if data is invalid if(request.getAttribute("error")!=null) { bean = (UserBean)request.getAttribute("error"); } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form method="post" action="ControllerServlet"> <CENTER> <TABLE border="0"width="600px"> <TR> <TD width="150px">Name:</TD> <TD> <INPUT TYPE="text" NAME="userName" value="<%=bean.getUserName()%>"> </TD> <TD width="350px"> <font color="red"><%=bean.getUserNameError()%> </font> </TD> </TR> <TR> <TD width="150px">Date Of Birth:</TD> <TD> <INPUT TYPE="text" NAME="dateOfBirth" value="<%=bean.getDateOfBirth()%>"> </TD> <TD> <font color="red"><%=bean.getDateOfBirthError()%> </font> </TD> </TR> <TR> <TD width="150px">E-Mail</TD> <TD> <INPUT TYPE="text" NAME="email" value="<%=bean.getEmail()%>"> </TD> <TD> <font color="red"><%=bean.getEmailError()%> </font> </TD> </TR> <TR> <TD width="150px">Phone no:</TD>

56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75.

<TD> <INPUT TYPE="text" NAME="phoneNo" value="<%=bean.getPhoneNo()%>"> </TD> <TD> <font color="red"><%=bean.getPhoneNoError()%> </font> </TD> </TR> <TR> <TD colspan="2" align="center"> <INPUT TYPE="submit" value="<%=bean.getAction()%>" name="action"> </TD> <TD> &nbsp; </TD> </TR> </TABLE> </CENTER> </form> </body> </html>

here we have created two objects. one is for displaying empty fields when we run jsp page first time . and second one is for displaying errors . if you have any doubts post your comment below we did not do anything just get the request attribute from servlet and display errors using bean methods now run insertupdate.jsp page before submit

After Submit with empty fields

in next post we will learn if these values are valid then insert values to database .

8. Creating Database Class


create a package name as database and create a class in that database DBClass
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9.

package database; /** * * @author Jagadeesh */ public class DBClass { }

in this package we will create methods for insert,delete,update


view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37.

package database; import import import import import import import import java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement; java.sql.ResultSet; java.sql.SQLException; java.util.ArrayList; java.util.List; model.UserBean;

/** * * @author Jagadeesh */ public class DBClass { public Connection createConnection() throws ClassNotFoundException,SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection ("jdbc:mysql://localhost:3306/userdbase", "root", "root"); return connection; } public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException { //here we will write code for insert } public List getAlldetails()throws SQLException, ClassNotFoundException { //here we will write code to get all records from database } public UserBean getDetails(int uid)throws SQLException, ClassNotFoundException { //here we will write code to get a single record from database } public int UpateDetails(int uid)throws SQLException, ClassNotFoundException

38. 39. 40. 41. }

{ //here we will write code to update a record }

here getAlldetails will return more than one user details so we returned list object. getDetails will return only one user details so one bean object is enough. here I used mysql fourth driver so no need to create any dsn name. You have to copy mysqlconnector.jar files to tomcat commons library. If you dont want to use this you can use odbc jdbc bridge driver in next post we will insert values into database

Insert Values
in this post we will call insertdetails method in servlet
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package database; import import import import import import import import java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement; java.sql.ResultSet; java.sql.SQLException; java.util.ArrayList; java.util.List; model.UserBean;

/** * * @author Jagadeesh */ public class DBClass { public Connection createConnection() throws ClassNotFoundException,SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager. getConnection("jdbc:mysql://localhost:3306/userdbase", "root", "root"); return connection; } //we get values from servlet by passing bean object to insertdetails method public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException { Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("insert into userdetails values(?,? ,?,?)"); //set values to prepared statement object by getting values from bean object pstmt.setString(1,bb.getUserName()); pstmt.setString(2,bb.getDateOfBirth()); pstmt.setString(3,bb.getEmail()); pstmt.setString(4,bb.getPhoneNo()); int i = pstmt.executeUpdate(); return i; } public List getAlldetails()throws SQLException, ClassNotFoundException { //here we will write code to get all records from database Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("select * from userdetails"); ResultSet rs = pstmt.executeQuery(); List list = new ArrayList(); while(rs.next()) { UserBean ubean = new UserBean(); ubean.setUserName(rs.getString(1)); ubean.setDateOfBirth(rs.getString(2));

55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. }

ubean.setEmail(rs.getString(3)); ubean.setPhoneNo(rs.getString(4)); list.add(ubean); } return list; } /* public UserBean getDetails(int uid)throws SQLException, ClassNotFoundException { //here we will write code to get a single record from database } public int UpateDetails(int uid)throws SQLException, ClassNotFoundException { //here we will write code to update a record }*/

now call that insertdetails method in servlet


view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37.

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.UserBean; import validation.ValidateForm; import database.DBClass; /** * * @author Jagadeesh */ public class ControllerServlet extends HttpServlet { //declare values to get form values from jsp page String userName; String dateOfBirth; String email; String phoneNo; String action; UserBean bean = new UserBean(); ValidateForm validateform = new ValidateForm(); DBClass dbobject = new DBClass(); protected void processRequest(HttpServletRequest request, HttpServletResponse respo nse) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { //get the values from jsp page userName = request.getParameter("userName"); dateOfBirth = request.getParameter("dateOfBirth"); email = request.getParameter("email"); phoneNo = request.getParameter("phoneNo"); action = request.getParameter("action");

38. 39. 40. 41. 42. 43. 44. 45. 46. 47. o 48. 49. 50. ; 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95.

if(action.equals("submit")) { //set values to bean.For this call below method setValuesToBean(); //check all form values are valid or not. send bean object UserBean checkedbean = validateform.validateData(bean); if(!checkedbean.getIsValid()) { //if data is invalid.set bean object in request and pass that request t //insertupdate.jsp using forward request.setAttribute("error",checkedbean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp") rd.forward(request, response); //now display errors in that jsp page } else { //using DBClass object call insertDetails method and pass bean object dbobject.insertDetails(bean); List list = dbobject.getAlldetails(); request.setAttribute("list", list); //forward to insertupdate page using requestdispatcher RequestDispatcher rd= request.getRequestDispatcher("view.jsp"); //display a message to client.store message in request object //forwarding to jsp rd.forward(request, response); } } } catch(Exception e) { out.println(e); } finally { out.close(); } } //this method is used to setvalues to bean public void setValuesToBean() { bean.setUserName(userName); bean.setDateOfBirth(dateOfBirth); bean.setEmail(email); bean.setPhoneNo(phoneNo); }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

96. 97. 98. 99. 100. 101. 102. 103. 104.

processRequest(request, response); }

public String getServletInfo() { return "Short description"; } }

9. Display all records in JSP


view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43.

<%-Document : view Created on : Mar 8, 2010, 10:49:03 AM Author : Jagadeesh --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="java.util.*" %> <%@page import="model.UserBean" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <a href="<%=request.getContextPath()%>/insertupdate.jsp">back</a> <table width="100%"border="1"> <tr> <th>Usename</th> <th>date of birth</th> <th>email</th> <th>phone no</th> <th>action</th> </tr> <% List list = (List)request.getAttribute("list"); if(list!=null) { for(int i=0 ; i< list.size();i++) { UserBean ubean =(UserBean) list.get(i); %> <tr> <td><%=ubean.getUserName()%></td> <td><%=ubean.getDateOfBirth()%></td> <td><%=ubean.getEmail()%></td> <td><%=ubean.getPhoneNo()%></td> <td> <a href="ControllerServlet?action=edit&userName=<%=ubean.getUserN ame()%>">update</a>| <a href="ControllerServlet?action=delete&userName=<%=ubean.getUse rName()%>">delete</a></td>

44. 45. </tr> 46. <% 47. } 48. } 49. %> 50. </table> 51. </body> 52. </html> now run insertupdate.jsp page and insert details then it will give following output with your database details

in next post we will update record.

10. Get details to update record


now in this post get command from view.jsp page and if that is equal to update then get user details using that name and display those details in insertupdate.jsp page and automatically the submit button change to update button because that value is getAction. we will set that action as update in database code. 1. get username and action from view.jsp page 2.create a method getDetails in database class 3.get details in servlet and forward to insertupdate.jsp page 1.get details from view.jsp
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26.

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.UserBean; import validation.ValidateForm; import database.DBClass; import java.util.List; /** * * @author Jagadeesh */ public class ControllerServlet extends HttpServlet { //declare values to get form values from jsp page String userName; String dateOfBirth; String email; String phoneNo; String action; UserBean bean = new UserBean(); ValidateForm validateform = new ValidateForm();

27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52.

DBClass dbobject = new DBClass(); protected void processRequest(HttpServletRequest request, HttpServletResponse respo nse) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { //get the values from jsp page userName = request.getParameter("userName"); dateOfBirth = request.getParameter("dateOfBirth"); email = request.getParameter("email"); phoneNo = request.getParameter("phoneNo"); action = request.getParameter("action"); if(action.equals("submit")) { //set values to bean.For this call below method setValuesToBean(); //check all form values are valid or not. send bean object UserBean checkedbean = validateform.validateData(bean); if(!checkedbean.getIsValid()) { //if data is invalid.set bean object in request and pass that request t o //insertupdate.jsp using forward request.setAttribute("error",checkedbean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp") ; rd.forward(request, response); //now display errors in that jsp page } else { //using DBClass object call insertDetails method and pass bean object dbobject.insertDetails(bean); List list = dbobject.getAlldetails(); request.setAttribute("list", list); //forward to insertupdate page using requestdispatcher RequestDispatcher rd= request.getRequestDispatcher("view.jsp"); //display a message to client.store message in request object //forwarding to jsp rd.forward(request, response);

53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82.

} } if(action.equals("edit")) { //get userdetails of particular name UserBean ubean = dbobject.getDetails(userName); request.setAttribute("updateuser",ubean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp"); rd.forward(request, response); } } catch(Exception e) { out.println(e);

83. } 84. finally { 85. out.close(); 86. } 87. } 88. //this method is used to setvalues to bean 89. public void setValuesToBean() 90. { 91. bean.setUserName(userName); 92. bean.setDateOfBirth(dateOfBirth); 93. bean.setEmail(email); 94. bean.setPhoneNo(phoneNo); 95. 96. } 97. 98. 99. protected void doGet(HttpServletRequest request, HttpServletResponse response) 100. throws ServletException, IOException { 101. processRequest(request, response); 102. } 103. 104. 105. protected void doPost(HttpServletRequest request, HttpServletResponse respon se) 106. throws ServletException, IOException { 107. processRequest(request, response); 108. } 109. 110. 111. public String getServletInfo() { 112. return "Short description"; 113. } 114. 115. }

2. create a method in database class


view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package database; import import import import import import import import java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement; java.sql.ResultSet; java.sql.SQLException; java.util.ArrayList; java.util.List; model.UserBean;

/** * * @author Jagadeesh */ public class DBClass {

22. public Connection createConnection() throws ClassNotFoundException,SQLException 23. { 24. Class.forName("com.mysql.jdbc.Driver"); 25. Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:330 6/userdbase", "root", "root"); 26. return connection; 27. } 28. //we get values from servlet by passing bean object to insertdetails method 29. public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. { Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("insert into userdetails values( ?,?,?,?)"); //set values to prepared statement object by getting values from bean object pstmt.setString(1,bb.getUserName()); pstmt.setString(2,bb.getDateOfBirth()); pstmt.setString(3,bb.getEmail()); pstmt.setString(4,bb.getPhoneNo()); int i = pstmt.executeUpdate(); return i; } public List getAlldetails()throws SQLException, ClassNotFoundException { Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("select * from userdetails"); ResultSet rs = pstmt.executeQuery(); List list = new ArrayList(); while(rs.next()) { UserBean ubean = new UserBean(); ubean.setUserName(rs.getString(1)); ubean.setDateOfBirth(rs.getString(2)); ubean.setEmail(rs.getString(3)); ubean.setPhoneNo(rs.getString(4)); list.add(ubean); } return list; } public UserBean getDetails(String uname)throws SQLException, ClassNotFoundException { //here we will write code to get a single record from database Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("select * from userdetails where user_name=?"); pstmt.setString(1, uname); ResultSet rs = pstmt.executeQuery(); List list = new ArrayList(); UserBean ubean = new UserBean(); while(rs.next()) { ubean.setUserName(rs.getString(1)); ubean.setDateOfBirth(rs.getString(2)); ubean.setEmail(rs.getString(3)); ubean.setPhoneNo(rs.getString(4)); }

78. ubean.setAction("update"); 79. return ubean; 80. } 81. /* public int UpateDetails(int uid)throws SQLException, ClassNotFoundException 82. { 83. //here we will write code to update a record 84. }*/ 85. } now display selected record in insertupdate.jsp page
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45.

<%-Document Author --%>

: index : Jagadeesh

<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="model.UserBean" %> <% UserBean bean; bean = new UserBean(); //get request from servlet if data is invalid if(request.getAttribute("error")!=null) { bean = (UserBean)request.getAttribute("error"); } if(request.getAttribute("updateuser")!=null) { bean = (UserBean)request.getAttribute("updateuser"); } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form method="post" action="ControllerServlet"> <CENTER> <TABLE border="0"width="600px"> <TR> <TD width="150px">Name:</TD> <TD> <INPUT TYPE="text" NAME="userName" value="<%=bean.getUserNa me()%>"> </TD> <TD width="350px"> <font color="red"><%=bean.getUserNameError()%> &nbsp;</font > </TD> </TR> <TR> <TD width="150px">Date Of Birth:</TD> <TD> <INPUT TYPE="text" NAME="dateOfBirth" value="<%=bean.getDat eOfBirth()%>" <%=request.getAttribute("updateuser")!=null?"readonly":""%>> </TD>

46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80.

<TD> <font color="red"><%=bean.getDateOfBirthError()%> </font> </TD> </TR> <TR> <TD width="150px">E-Mail</TD> <TD> <INPUT TYPE="text" NAME="email" value="<%=bean.getEmail()%> "> </TD> <TD> <font color="red"><%=bean.getEmailError()%> </font> </TD> </TR> <TR> <TD width="150px">Phone no:</TD> <TD> <INPUT TYPE="text" NAME="phoneNo" value="<%=bean.getPhoneNo ()%>"> </TD> <TD> <font color="red"><%=bean.getPhoneNoError()%> </font> </TD> </TR> <TR> <TD colspan="2" align="center"> <INPUT TYPE="submit" value="<%=bean.getAction()%>" name="ac tion"> </TD> <TD> &nbsp; </TD> </TR> </TABLE> </CENTER> </form> </body> </html>

run insertupdate.jsp page and enter somedetails and it will display all fidleda and select one record then it wil show following output with update button

in this post we got all details for selected user. in next post we will update that user details

11. Update selected record


in this post we will create method for uploading record
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53.

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.UserBean; import validation.ValidateForm; import database.DBClass; import java.util.List; /** * * @author Jagadeesh */ public class ControllerServlet extends HttpServlet { //declare values to get form values from jsp page String userName; String dateOfBirth; String email; String phoneNo; String action; UserBean bean = new UserBean(); ValidateForm validateform = new ValidateForm(); DBClass dbobject = new DBClass(); protected void processRequest(HttpServletRequest request, HttpServletResponse respo nse) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { //get the values from jsp page userName = request.getParameter("userName"); dateOfBirth = request.getParameter("dateOfBirth"); email = request.getParameter("email"); phoneNo = request.getParameter("phoneNo"); action = request.getParameter("action"); if(action.equals("submit")) { //set values to bean.For this call below method setValuesToBean(); //check all form values are valid or not. send bean object UserBean checkedbean = validateform.validateData(bean); if(!checkedbean.getIsValid()) { //if data is invalid.set bean object in request and pass that request t o //insertupdate.jsp using forward checkedbean.setAction("submit"); request.setAttribute("error",checkedbean);

54. ; 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. o 89. 90. 91. 92. ; 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107.

RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp") rd.forward(request, response); //now display errors in that jsp page } else { //using DBClass object call insertDetails method and pass bean object dbobject.insertDetails(bean); List list = dbobject.getAlldetails(); request.setAttribute("list", list); //forward to insertupdate page using requestdispatcher RequestDispatcher rd= request.getRequestDispatcher("view.jsp"); //display a message to client.store message in request object //forwarding to jsp rd.forward(request, response);

} } if(action.equals("edit")) { //get userdetails of particular name UserBean ubean = dbobject.getDetails(userName); request.setAttribute("updateuser",ubean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp"); rd.forward(request, response); } if(action.equals("update")) { setValuesToBean(); UserBean checkedbean = validateform.validateData(bean); if(!checkedbean.getIsValid()) { //if data is invalid.set bean object in request and pass that request t //insertupdate.jsp using forward checkedbean.setAction("update"); request.setAttribute("error",checkedbean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp") rd.forward(request, response); //now display errors in that jsp page } else { //using DBClass object call insertDetails method and pass bean object dbobject.UpateDetails(bean,userName); List list = dbobject.getAlldetails(); request.setAttribute("list", list); //forward to insertupdate page using requestdispatcher RequestDispatcher rd= request.getRequestDispatcher("view.jsp"); //display a message to client.store message in request object //forwarding to jsp rd.forward(request, response);

108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. e) 132. 133. 134. 135. 136. 137. se) 138. 139. 140. 141. 142. 143. 144. 145. 146. 147.

} } } catch(Exception e) { out.println(e); } finally { out.close(); } } //this method is used to setvalues to bean public void setValuesToBean() { bean.setUserName(userName); bean.setDateOfBirth(dateOfBirth); bean.setEmail(email); bean.setPhoneNo(phoneNo); }

protected void doGet(HttpServletRequest request, HttpServletResponse respons throws ServletException, IOException { processRequest(request, response); }

protected void doPost(HttpServletRequest request, HttpServletResponse respon throws ServletException, IOException { processRequest(request, response); }

public String getServletInfo() { return "Short description"; } }

creating updatedetails method in dbclass


view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package database; import import import import import import import java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement; java.sql.ResultSet; java.sql.SQLException; java.util.ArrayList; java.util.List;

15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70.

import model.UserBean; /** * * @author Jagadeesh */ public class DBClass { public Connection createConnection() throws ClassNotFoundException,SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:330 6/userdbase", "root", "root"); return connection; } //we get values from servlet by passing bean object to insertdetails method public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException { Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("insert into userdetails values( ?,?,?,?)"); //set values to prepared statement object by getting values from bean object pstmt.setString(1,bb.getUserName()); pstmt.setString(2,bb.getDateOfBirth()); pstmt.setString(3,bb.getEmail()); pstmt.setString(4,bb.getPhoneNo()); int i = pstmt.executeUpdate(); return i; } public List getAlldetails()throws SQLException, ClassNotFoundException { Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("select * from userdetails"); ResultSet rs = pstmt.executeQuery(); List list = new ArrayList(); while(rs.next()) { UserBean ubean = new UserBean(); ubean.setUserName(rs.getString(1)); ubean.setDateOfBirth(rs.getString(2)); ubean.setEmail(rs.getString(3)); ubean.setPhoneNo(rs.getString(4)); list.add(ubean); } return list; } public UserBean getDetails(String uname)throws SQLException, ClassNotFoundException { //here we will write code to get a single record from database Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("select * from userdetails where user_name=?"); pstmt.setString(1, uname); ResultSet rs = pstmt.executeQuery(); List list = new ArrayList(); UserBean ubean = new UserBean(); while(rs.next())

71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81.

{ ubean.setUserName(rs.getString(1)); ubean.setDateOfBirth(rs.getString(2)); ubean.setEmail(rs.getString(3)); ubean.setPhoneNo(rs.getString(4)); } ubean.setAction("update"); return ubean;

} public void UpateDetails(UserBean ubean, String name)throws SQLException, ClassNot FoundException 82. { 83. 84. Connection con = createConnection(); 85. PreparedStatement pstmt = con.prepareStatement("update userdetails set date_of_ birth=?,e_mail=?,phone_no=? where user_name=? "); 86. //set values to prepared statement object by getting values from bean object 87. pstmt.setString(1,ubean.getDateOfBirth()); 88. pstmt.setString(2,ubean.getEmail()); 89. pstmt.setString(3,ubean.getPhoneNo()); 90. pstmt.setString(4,name); 91. pstmt.executeUpdate(); 92. 93. 94. } 95. } run insertupdate.jsp page and insert record.then it will show all records

after selecting one record

after updating record

12. Delete selected record


in this post we will delete selected record from view.jsp page for this we will create deleteDetails method in dbclass
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52.

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.UserBean; import validation.ValidateForm; import database.DBClass; import java.util.List; /** * * @author Jagadeesh */ public class ControllerServlet extends HttpServlet { //declare values to get form values from jsp page String userName; String dateOfBirth; String email; String phoneNo; String action; UserBean bean = new UserBean(); ValidateForm validateform = new ValidateForm(); DBClass dbobject = new DBClass(); protected void processRequest(HttpServletRequest request, HttpServletResponse respo nse) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { //get the values from jsp page userName = request.getParameter("userName"); dateOfBirth = request.getParameter("dateOfBirth"); email = request.getParameter("email"); phoneNo = request.getParameter("phoneNo"); action = request.getParameter("action"); if(action.equals("submit")) { //set values to bean.For this call below method setValuesToBean(); //check all form values are valid or not. send bean object UserBean checkedbean = validateform.validateData(bean); if(!checkedbean.getIsValid()) { //if data is invalid.set bean object in request and pass that request t o //insertupdate.jsp using forward checkedbean.setAction("submit"); request.setAttribute("error",checkedbean);

53. 54. ; 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. o 89. 90. 91. 92. ; 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106.

RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp") rd.forward(request, response); //now display errors in that jsp page } else { //using DBClass object call insertDetails method and pass bean object dbobject.insertDetails(bean); List list = dbobject.getAlldetails(); request.setAttribute("list", list); //forward to insertupdate page using requestdispatcher RequestDispatcher rd= request.getRequestDispatcher("view.jsp"); //display a message to client.store message in request object //forwarding to jsp rd.forward(request, response);

} } if(action.equals("edit")) { //get userdetails of particular name UserBean ubean = dbobject.getDetails(userName); request.setAttribute("updateuser",ubean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp"); rd.forward(request, response); } if(action.equals("update")) { setValuesToBean(); UserBean checkedbean = validateform.validateData(bean); if(!checkedbean.getIsValid()) { //if data is invalid.set bean object in request and pass that request t //insertupdate.jsp using forward checkedbean.setAction("update"); request.setAttribute("error",checkedbean); RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp") rd.forward(request, response); //now display errors in that jsp page } else { //using DBClass object call insertDetails method and pass bean object dbobject.UpateDetails(bean,userName); List list = dbobject.getAlldetails(); request.setAttribute("list", list); //forward to insertupdate page using requestdispatcher RequestDispatcher rd= request.getRequestDispatcher("view.jsp"); //display a message to client.store message in request object //forwarding to jsp rd.forward(request, response);

107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. e) 140. 141. 142. 143. 144. 145. se) 146. 147. 148. 149. 150. 151. 152. 153. 154. 155.

} } if(action.equals("delete")) { //delete userdetails of particular name dbobject.deleteDetails(userName); RequestDispatcher rd = request.getRequestDispatcher("view.jsp"); rd.forward(request, response); } } catch(Exception e) { out.println(e); } finally { out.close(); } } //this method is used to setvalues to bean public void setValuesToBean() { bean.setUserName(userName); bean.setDateOfBirth(dateOfBirth); bean.setEmail(email); bean.setPhoneNo(phoneNo); }

protected void doGet(HttpServletRequest request, HttpServletResponse respons throws ServletException, IOException { processRequest(request, response); }

protected void doPost(HttpServletRequest request, HttpServletResponse respon throws ServletException, IOException { processRequest(request, response); }

public String getServletInfo() { return "Short description"; } }

create method in DBClass

/* * To change this template, choose Tools | Templates * and open the template in the editor.

*/

package database;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import model.UserBean;

/** * * @author Jagadeesh */ public class DBClass { public Connection createConnection() throws ClassNotFoundException,SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdbase", "root", "root"); return connection; } //we get values from servlet by passing bean object to insertdetails method public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException { Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("insert into userdetails values(?,?,?,?)"); //set values to prepared statement object by getting values from bean object pstmt.setString(1,bb.getUserName()); pstmt.setString(2,bb.getDateOfBirth()); pstmt.setString(3,bb.getEmail()); pstmt.setString(4,bb.getPhoneNo()); int i = pstmt.executeUpdate(); return i;

} public List getAlldetails()throws SQLException, ClassNotFoundException {

Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("select * from userdetails"); ResultSet rs = pstmt.executeQuery(); List list = new ArrayList(); while(rs.next()) { UserBean ubean = new UserBean(); ubean.setUserName(rs.getString(1)); ubean.setDateOfBirth(rs.getString(2)); ubean.setEmail(rs.getString(3)); ubean.setPhoneNo(rs.getString(4)); list.add(ubean);

} return list; } public UserBean getDetails(String uname)throws SQLException, ClassNotFoundException { //here we will write code to get a single record from database Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("select * from userdetails where user_name=?"); pstmt.setString(1, uname); ResultSet rs = pstmt.executeQuery(); List list = new ArrayList(); UserBean ubean = new UserBean(); while(rs.next()) {

ubean.setUserName(rs.getString(1)); ubean.setDateOfBirth(rs.getString(2)); ubean.setEmail(rs.getString(3)); ubean.setPhoneNo(rs.getString(4)); } ubean.setAction("update");

return ubean; } public void UpateDetails(UserBean ubean, String name)throws SQLException, ClassNotFoundException {

Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("update userdetails set date_of_birth=?,e_mail=?,phone_no=? where user_name=? "); //set values to prepared statement object by getting values from bean object pstmt.setString(1,ubean.getDateOfBirth()); pstmt.setString(2,ubean.getEmail()); pstmt.setString(3,ubean.getPhoneNo()); pstmt.setString(4,name); pstmt.executeUpdate();

} public void deleteDetails(String uname)throws SQLException, ClassNotFoundException { //here we will write code to get a single record from database Connection con = createConnection(); PreparedStatement pstmt = con.prepareStatement("delete * from userdetails where user_name=?"); pstmt.setString(1, uname);

} in next post we will see all files we created