Date post: | 08-Aug-2015 |
Category: |
Documents |
Upload: | ankit-jain |
View: | 32 times |
Download: | 0 times |
1. Creating insertupdate.jsp page2. 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 nextnow 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. package model; 2. 3. /** 4. * 5. * @author Jagadeesh 6. */ 7. public class UserBean { 8. 9. }
now insert setter methods and getter methods in that been.Simple way to create setter methods and getter methods.
1. Declare variables2. 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. package model; 2. package model; 3. 4. /** 5. * @author Jagadeesh 6. */ 7. public class UserBean { 8. 9. private String userName ; 10. private String dateOfBirth ;
11. private String email ; 12. private String phoneNo ; 13. 14. //for validation error messages 15. 16. private String userNameError; 17. private String dateOfBirthError; 18. private String emailError; 19. private String phoneNoError; 20. private boolean isValid; 21. 22. //this is for action means edit or submit or delete or update 23. private String action; 24. //construtor.it is invoked when we run jsp page. 25. //so first time no values will display in jsp page 26. public UserBean() 27. { 28. userName =""; 29. dateOfBirth =""; 30. email=""; 31. phoneNo=""; 32. 33. userNameError=""; 34. dateOfBirthError=""; 35. emailError=""; 36. phoneNoError=""; 37. 38. action = "submit"; 39. } 40. 41. public String getDateOfBirth() { 42. return dateOfBirth; 43. } 44. 45. public void setDateOfBirth(String dateOfBirth) { 46. this.dateOfBirth = dateOfBirth; 47. } 48. 49. public String getEmail() { 50. return email; 51. } 52. 53. public void setEmail(String email) { 54. this.email = email; 55. } 56. 57. public String getPhoneNo() { 58. return phoneNo; 59. } 60. 61. public void setPhoneNo(String phoneNo) { 62. this.phoneNo = phoneNo; 63. } 64. 65. public String getUserName() { 66. return userName; 67. } 68. 69. public void setUserName(String userName) { 70. this.userName = userName; 71. }
72. 73. //setter and getter methods for errors 74. 75. public String getUserNameError() { 76. return userNameError; 77. } 78. 79. public void setUserNameError(String userNameError) { 80. this.userNameError = userNameError; 81. } 82. public String getDateOfBirthError() { 83. return dateOfBirthError; 84. } 85. 86. public void setDateOfBirthError(String dateOfBirthError) { 87. this.dateOfBirthError = dateOfBirthError; 88. } 89. 90. public String getEmailError() { 91. return emailError; 92. } 93. 94. public void setEmailError(String emailError) { 95. this.emailError = emailError; 96. } 97. 98. public String getPhoneNoError() { 99. return phoneNoError; 100. } 101. 102. public void setPhoneNoError(String phoneNoError) { 103. this.phoneNoError = phoneNoError; 104. } 105. 106. public boolean getIsValid() { 107. return isValid; 108. } 109. public void setIsValid(boolean isValid) { 110. this.isValid = isValid; 111. } 112. 113. public String getAction() { 114. return action; 115. } 116. public void setAction(String action) { 117. this.action = action; 118. } 119. 120. }
in next post we will create a class for validate form
4. Creating validation class
Up to now we createdinsertupdate.jspControllerServlet.javapackage model and bean classnow in this post we will create a package and a classcrate a package validation and with in this package create a class ValidateForm.
1. package validation; 2. 3. /** 4. * 5. * @author Jagadeesh 6. */ 7. public class ValidateForm { 8. 9. 10. }
now go to servlet. in that servlet declare variables to get form values and create a method for setting values to bean
1. import java.io.IOException; 2. import java.io.PrintWriter; 3. import javax.servlet.RequestDispatcher; 4. import javax.servlet.ServletException; 5. import javax.servlet.http.HttpServlet; 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. import model.UserBean; 9. import validation.ValidateForm; 10. 11. /** 12. * 13. * @author Jagadeesh 14. */ 15. public class ControllerServlet extends HttpServlet { 16. 17. //declare values to get form values from jsp page 18. String userName; 19. String dateOfBirth; 20. String email; 21. String phoneNo; 22. String action; 23. 24. UserBean bean = new UserBean(); 25. ValidateForm validateform = new ValidateForm(); 26. 27. protected void processRequest(HttpServletRequest request, HttpServletRespon
se response) 28. throws ServletException, IOException { 29. response.setContentType("text/html;charset=UTF-8"); 30. PrintWriter out = response.getWriter(); 31. try { 32. //get the values from jsp page 33. userName = request.getParameter("userName"); 34. dateOfBirth = request.getParameter("dateOfBirth"); 35. email = request.getParameter("email"); 36. phoneNo = request.getParameter("phoneNo"); 37. action = request.getParameter("action");
38. if(action.equals("submit")) 39. { 40. //set values to bean.For this call below method 41. setValuesToBean(); 42. 43. 44. } 45. } 46. catch(Exception e) 47. { 48. out.println(e); 49. } 50. finally { 51. out.close(); 52. } 53. } 54. //this method is used to setvalues to bean 55. public void setValuesToBean() 56. { 57. bean.setUserName(userName); 58. bean.setDateOfBirth(dateOfBirth); 59. bean.setEmail(email); 60. bean.setPhoneNo(phoneNo); 61. 62. } 63. 64. 65. protected void doGet(HttpServletRequest request, HttpServletResponse respon
se) 66. throws ServletException, IOException { 67. processRequest(request, response); 68. } 69. 70. 71. protected void doPost(HttpServletRequest request, HttpServletResponse respo
nse) 72. throws ServletException, IOException { 73. processRequest(request, response); 74. } 75. 76. 77. public String getServletInfo() { 78. return "Short description"; 79. } 80. 81. }
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. * To change this template, choose Tools | Templates 3. * and open the template in the editor. 4. */ 5. 6. package validation; 7. import model.UserBean; 8. /** 9. * 10. * @author Jagadeesh 11. */ 12. public class ValidateForm { 13. 14. 15. UserBean validBean = new UserBean(); 16. public UserBean validateData(UserBean ubean) 17. { 18. 19. //if given username value is not valid then seterror 20. // message and set empty value to input field 21. if(ubean.getUserName().length()==0) 22. { 23. validBean.setUserNameError("please enter valid name"); 24. validBean.setIsValid(false); 25. validBean.setUserName(""); 26. } 27. else 28. if(!ubean.getUserName().matches("[a-zA-Z]*")) 29. { 30. validBean.setUserNameError("please enter valid name"); 31. validBean.setIsValid(false); 32. validBean.setUserName(""); 33. 34. } 35. //if given username value is valid then seterror message to 36. // empty and set value to bean 37. else 38. { 39. validBean.setUserNameError(""); 40. validBean.setIsValid(true); 41. validBean.setUserName(ubean.getUserName()); 42. } 43. 44. 45. //if given username value is not valid then seterror message 46. // and set empty value to input field 47. if(!ubean.getDateOfBirth().matches("\\d{1,2}-\\d{1,2}-\\d{4}")) 48. { 49. validBean.setDateOfBirthError("please enter valid date"); 50. validBean.setIsValid(false); 51. validBean.setDateOfBirth(""); 52.
53. } 54. //if given username value is valid then seterror message 55. // to empty and set value to bean 56. else 57. { 58. validBean.setDateOfBirthError(""); 59. validBean.setIsValid(true); 60. validBean.setDateOfBirth(ubean.getDateOfBirth()); 61. } 62. 63. 64. //if given username value is not valid then seterror message 65. //and set empty value to input field 66. 67. if(!ubean.getEmail().matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\
w]+[\\w]$")) 68. { 69. validBean.setEmailError("please enter valid email"); 70. validBean.setIsValid(false); 71. validBean.setEmail(""); 72. } 73. //if given username value is valid then seterror message 74. //to empty and set value to bean 75. else 76. { 77. validBean.setEmailError(""); 78. validBean.setIsValid(true); 79. validBean.setEmail(ubean.getEmail()); 80. } 81. 82. //if given username value is not valid then seterror message 83. // and set empty value to input field 84. if(!ubean.getPhoneNo().matches("\\d{10}")) 85. { 86. validBean.setPhoneNoError("please enter valid phoneno"); 87. validBean.setIsValid(false); 88. validBean.setPhoneNo(""); 89. } 90. //if given username value is valid then seterror message to 91. // empty and set value to bean 92. else 93. { 94. validBean.setPhoneNoError(""); 95. validBean.setIsValid(true); 96. validBean.setPhoneNo(ubean.getPhoneNo()); 97. } 98. return validBean; 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 plain print ?
1. import java.io.IOException; 2. import java.io.PrintWriter; 3. import javax.servlet.RequestDispatcher; 4. import javax.servlet.ServletException; 5. import javax.servlet.http.HttpServlet; 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. import model.UserBean; 9. import validation.ValidateForm; 10. 11. /** 12. * 13. * @author Jagadeesh 14. */ 15. 16. public class ControllerServlet extends HttpServlet { 17. 18. //declare values to get form values from jsp page 19. String userName; 20. String dateOfBirth; 21. String email; 22. String phoneNo; 23. String action; 24. 25. UserBean bean = new UserBean(); 26. ValidateForm validateform = new ValidateForm(); 27. 28. protected void processRequest(HttpServletRequest request, HttpServletRespon
se response) 29. throws ServletException, IOException { 30. response.setContentType("text/html;charset=UTF-8"); 31. PrintWriter out = response.getWriter(); 32. try { 33. //get the values from jsp page 34. userName = request.getParameter("userName"); 35. dateOfBirth = request.getParameter("dateOfBirth"); 36. email = request.getParameter("email"); 37. phoneNo = request.getParameter("phoneNo"); 38. action = request.getParameter("action"); 39. if(action.equals("submit")) 40. { 41. //set values to bean.For this call below method 42. setValuesToBean(); 43. 44. //check all form values are valid or not. send bean object 45. UserBean checkedbean = validateform.validateData(bean); 46. if(!checkedbean.getIsValid()) 47. { 48. //if data is invalid.set bean object in request and pass that r
equest to 49. //insertupdate.jsp using forward 50. checkedbean.setAction("submit");
51. request.setAttribute("error",checkedbean); 52. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 53. rd.forward(request, response); 54. //now display errors in that jsp page 55. } 56. } 57. } 58. catch(Exception e) 59. { 60. out.println(e); 61. } 62. finally { 63. out.close(); 64. } 65. } 66. //this method is used to setvalues to bean 67. public void setValuesToBean() 68. { 69. bean.setUserName(userName); 70. bean.setDateOfBirth(dateOfBirth); 71. bean.setEmail(email); 72. bean.setPhoneNo(phoneNo); 73. 74. }
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 errorsview plainprint?
1. <%-- 2. Document : index 3. Author : Jagadeesh 4. --%> 5. 6. <%@page contentType="text/html" pageEncoding="UTF-8"%> 7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 8. "http://www.w3.org/TR/html4/loose.dtd"> 9. <%@page import="model.UserBean" %> 10. <% UserBean bean; 11. bean = new UserBean(); 12. //get request from servlet if data is invalid 13. if(request.getAttribute("error")!=null) 14. { 15. bean = (UserBean)request.getAttribute("error"); 16. } 17. %> 18. <html> 19. <head> 20. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 21. <title>JSP Page</title> 22. </head> 23. <body> 24. <form method="post" action="ControllerServlet"> 25. <CENTER> 26. <TABLE border="0"width="600px"> 27. <TR> 28. <TD width="150px">Name:</TD> 29. <TD> 30. <INPUT TYPE="text" NAME="userName" value="<%=bean.getUserName()%>"> 31. </TD> 32. <TD width="350px"> 33. <font color="red"><%=bean.getUserNameError()%> </font> 34. </TD> 35. </TR> 36. <TR> 37. <TD width="150px">Date Of Birth:</TD> 38. <TD> 39. <INPUT TYPE="text" NAME="dateOfBirth" value="<%=bean.getDateOfBirth()%>"> 40. </TD> 41. <TD> 42. <font color="red"><%=bean.getDateOfBirthError()%> </font> 43. </TD> 44. </TR> 45. <TR> 46. <TD width="150px">E-Mail</TD> 47. <TD> 48. <INPUT TYPE="text" NAME="email" value="<%=bean.getEmail()%>"> 49. </TD> 50. <TD> 51. <font color="red"><%=bean.getEmailError()%> </font> 52. </TD> 53. </TR> 54. <TR> 55. <TD width="150px">Phone no:</TD> 56. <TD>
57. <INPUT TYPE="text" NAME="phoneNo" value="<%=bean.getPhoneNo()%>"> 58. </TD> 59. <TD> 60. <font color="red"><%=bean.getPhoneNoError()%> </font> 61. </TD> 62. </TR> 63. <TR> 64. <TD colspan="2" align="center"> 65. <INPUT TYPE="submit" value="<%=bean.getAction()%>" name="action"> 66. </TD> 67. <TD> 68. 69. </TD> 70. </TR> 71. </TABLE> 72. </CENTER> 73. </form> 74. </body> 75. </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 belowwe did not do anything just get the request attribute from servlet and display errors using bean methodsnow 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. package database; 2. 3. /** 4. * 5. * @author Jagadeesh 6. */ 7. public class DBClass { 8. 9. }
in this package we will create methods for insert,delete,update
view plainprint?
1. package database; 2. 3. import java.sql.Connection; 4. import java.sql.DriverManager; 5. import java.sql.PreparedStatement; 6. import java.sql.ResultSet; 7. import java.sql.SQLException; 8. import java.util.ArrayList; 9. import java.util.List; 10. import model.UserBean; 11. 12. /** 13. * 14. * @author Jagadeesh 15. */ 16. public class DBClass { 17. public Connection createConnection() throws ClassNotFoundException,SQLExceptio
n 18. { 19. Class.forName("com.mysql.jdbc.Driver"); 20. Connection connection = DriverManager.getConnection 21. ("jdbc:mysql://localhost:3306/userdbase", "root", "root"); 22. return connection; 23. } 24. public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundEx
ception 25. { 26. //here we will write code for insert 27. 28. } 29. public List getAlldetails()throws SQLException, ClassNotFoundException 30. { 31. //here we will write code to get all records from database 32. } 33. public UserBean getDetails(int uid)throws SQLException, ClassNotFoundExcept
ion 34. { 35. //here we will write code to get a single record from database 36. }
37. public int UpateDetails(int uid)throws SQLException, ClassNotFoundException
38. { 39. //here we will write code to update a record 40. } 41. }
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 mysql-connector.jar files to tomcat commons library. If you don’t want to use this you can use odbc jdbc bridge driver
in next post we will insert values into database
Insert Valuesin this post we will call insertdetails method in servlet
view plainprint?
1. /* 2. * To change this template, choose Tools | Templates 3. * and open the template in the editor. 4. */ 5. 6. package database; 7. 8. import java.sql.Connection; 9. import java.sql.DriverManager; 10. import java.sql.PreparedStatement; 11. import java.sql.ResultSet; 12. import java.sql.SQLException; 13. import java.util.ArrayList; 14. import java.util.List; 15. import model.UserBean; 16. 17. /** 18. * 19. * @author Jagadeesh 20. */ 21. public class DBClass { 22. public Connection createConnection() throws ClassNotFoundException,SQLExceptio
n 23. { 24. Class.forName("com.mysql.jdbc.Driver"); 25. Connection connection = DriverManager. 26. getConnection("jdbc:mysql://localhost:3306/userdbase", "root", "root
"); 27. return connection; 28. } 29. //we get values from servlet by passing bean object to insertdetails method 30. public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundExce
ption 31. { 32. Connection con = createConnection(); 33. PreparedStatement pstmt = con.prepareStatement("insert into userdetails va
lues(?,?,?,?)"); 34. //set values to prepared statement object by getting values from bean obje
ct 35. pstmt.setString(1,bb.getUserName()); 36. pstmt.setString(2,bb.getDateOfBirth()); 37. pstmt.setString(3,bb.getEmail()); 38. pstmt.setString(4,bb.getPhoneNo()); 39. int i = pstmt.executeUpdate(); 40. return i; 41. 42. } 43. public List getAlldetails()throws SQLException, ClassNotFoundException 44. { 45. //here we will write code to get all records from database 46. Connection con = createConnection(); 47. PreparedStatement pstmt = con.prepareStatement("select * from userdetai
ls"); 48. ResultSet rs = pstmt.executeQuery(); 49. List list = new ArrayList(); 50. while(rs.next())
51. { 52. UserBean ubean = new UserBean(); 53. ubean.setUserName(rs.getString(1)); 54. ubean.setDateOfBirth(rs.getString(2)); 55. ubean.setEmail(rs.getString(3)); 56. ubean.setPhoneNo(rs.getString(4)); 57. list.add(ubean); 58. 59. } 60. return list; 61. } 62. /* public UserBean getDetails(int uid)throws SQLException, ClassNotFoundExce
ption 63. { 64. //here we will write code to get a single record from database 65. } 66. public int UpateDetails(int uid)throws SQLException, ClassNotFoundExceptio
n 67. { 68. //here we will write code to update a record 69. }*/ 70. }
now call that insertdetails method in servlet
view plainprint?
1. import java.io.IOException; 2. import java.io.PrintWriter; 3. import javax.servlet.RequestDispatcher; 4. import javax.servlet.ServletException; 5. import javax.servlet.http.HttpServlet; 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. import model.UserBean; 9. import validation.ValidateForm; 10. import database.DBClass; 11. /** 12. * 13. * @author Jagadeesh 14. */ 15. public class ControllerServlet extends HttpServlet { 16. 17. //declare values to get form values from jsp page 18. String userName; 19. String dateOfBirth; 20. String email; 21. String phoneNo; 22. String action; 23. 24. UserBean bean = new UserBean(); 25. ValidateForm validateform = new ValidateForm(); 26. DBClass dbobject = new DBClass(); 27. protected void processRequest(HttpServletRequest request, HttpServletRespon
se response) 28. throws ServletException, IOException { 29. response.setContentType("text/html;charset=UTF-8"); 30. PrintWriter out = response.getWriter(); 31. try {
32. //get the values from jsp page 33. userName = request.getParameter("userName"); 34. dateOfBirth = request.getParameter("dateOfBirth"); 35. email = request.getParameter("email"); 36. phoneNo = request.getParameter("phoneNo"); 37. action = request.getParameter("action"); 38. if(action.equals("submit")) 39. { 40. //set values to bean.For this call below method 41. setValuesToBean(); 42. 43. //check all form values are valid or not. send bean object 44. UserBean checkedbean = validateform.validateData(bean); 45. if(!checkedbean.getIsValid()) 46. { 47. //if data is invalid.set bean object in request and pass that r
equest to 48. //insertupdate.jsp using forward 49. request.setAttribute("error",checkedbean); 50. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 51. rd.forward(request, response); 52. //now display errors in that jsp page 53. } 54. else 55. { 56. //using DBClass object call insertDetails method and pass bea
n object 57. dbobject.insertDetails(bean); 58. List list = dbobject.getAlldetails(); 59. request.setAttribute("list", list); 60. //forward to insertupdate page using requestdispatcher 61. RequestDispatcher rd= request.getRequestDispatcher
("view.jsp"); 62. //display a message to client.store message in request object 63. //forwarding to jsp 64. rd.forward(request, response); 65. 66. } 67. } 68. } 69. catch(Exception e) 70. { 71. out.println(e); 72. } 73. finally { 74. out.close(); 75. } 76. } 77. //this method is used to setvalues to bean 78. public void setValuesToBean() 79. { 80. bean.setUserName(userName); 81. bean.setDateOfBirth(dateOfBirth); 82. bean.setEmail(email); 83. bean.setPhoneNo(phoneNo); 84. 85. } 86. 87.
88. protected void doGet(HttpServletRequest request, HttpServletResponse response)
89. throws ServletException, IOException { 90. processRequest(request, response); 91. } 92. 93. 94. protected void doPost(HttpServletRequest request, HttpServletResponse respo
nse) 95. throws ServletException, IOException { 96. processRequest(request, response); 97. } 98. 99. 100. public String getServletInfo() { 101. return "Short description"; 102. } 103. 104. }
9. Display all records in JSP
view plainprint?
1. <%-- 2. Document : view 3. Created on : Mar 8, 2010, 10:49:03 AM 4. Author : Jagadeesh 5. --%> 6. 7. <%@page contentType="text/html" pageEncoding="UTF-8"%> 8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 9. "http://www.w3.org/TR/html4/loose.dtd"> 10. <%@page import="java.util.*" %> 11. <%@page import="model.UserBean" %> 12. <html> 13. <head> 14. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 15. <title>JSP Page</title> 16. </head> 17. <body> 18. <a href="<%=request.getContextPath()%>/insertupdate.jsp">back</a> 19. <table width="100%"border="1"> 20. <tr> 21. <th>Usename</th> 22. <th>date of birth</th> 23. <th>email</th> 24. <th>phone no</th> 25. <th>action</th> 26. </tr> 27. <% 28. List list = (List)request.getAttribute("list"); 29. if(list!=null) 30. { 31. for(int i=0 ; i< list.size();i++) 32. { 33. UserBean ubean =(UserBean) list.get(i); 34. 35. %>
36. <tr> 37. <td><%=ubean.getUserName()%></td> 38. <td><%=ubean.getDateOfBirth()%></td> 39. <td><%=ubean.getEmail()%></td> 40. <td><%=ubean.getPhoneNo()%></td> 41. <td> 42. <a href="ControllerServlet?action=edit&userName=<
%=ubean.getUserName()%>">update</a>| 43. <a href="ControllerServlet?action=delete&userName=<
%=ubean.getUserName()%>">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 page2.create a method getDetails in database class3.get details in servlet and forward to insertupdate.jsp page
1.get details from view.jspview plainprint?
1. import java.io.IOException; 2. import java.io.PrintWriter; 3. import javax.servlet.RequestDispatcher; 4. import javax.servlet.ServletException; 5. import javax.servlet.http.HttpServlet; 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. import model.UserBean; 9. import validation.ValidateForm; 10. import database.DBClass; 11. import java.util.List; 12. /** 13. * 14. * @author Jagadeesh 15. */ 16. public class ControllerServlet extends HttpServlet {
17. 18. //declare values to get form values from jsp page 19. String userName; 20. String dateOfBirth; 21. String email; 22. String phoneNo; 23. String action; 24. 25. UserBean bean = new UserBean(); 26. ValidateForm validateform = new ValidateForm(); 27. DBClass dbobject = new DBClass(); 28. protected void processRequest(HttpServletRequest request, HttpServletRespon
se response) 29. throws ServletException, IOException { 30. response.setContentType("text/html;charset=UTF-8"); 31. PrintWriter out = response.getWriter(); 32. try { 33. //get the values from jsp page 34. userName = request.getParameter("userName"); 35. dateOfBirth = request.getParameter("dateOfBirth"); 36. email = request.getParameter("email"); 37. phoneNo = request.getParameter("phoneNo"); 38. action = request.getParameter("action"); 39. if(action.equals("submit")) 40. { 41. //set values to bean.For this call below method 42. setValuesToBean(); 43. 44. //check all form values are valid or not. send bean object 45. UserBean checkedbean = validateform.validateData(bean); 46. if(!checkedbean.getIsValid()) 47. { 48. 49. //if data is invalid.set bean object in request and pass that r
equest to 50. //insertupdate.jsp using forward 51. request.setAttribute("error",checkedbean); 52. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 53. rd.forward(request, response); 54. //now display errors in that jsp page 55. } 56. else 57. { 58. //using DBClass object call insertDetails method and pass bea
n object 59. dbobject.insertDetails(bean); 60. List list = dbobject.getAlldetails(); 61. request.setAttribute("list", list); 62. //forward to insertupdate page using requestdispatcher 63. RequestDispatcher rd= request.getRequestDispatcher
("view.jsp"); 64. //display a message to client.store message in request object 65. //forwarding to jsp 66. rd.forward(request, response); 67. 68. 69. } 70. } 71. if(action.equals("edit")) 72. {
73. //get userdetails of particular name 74. UserBean ubean = dbobject.getDetails(userName); 75. request.setAttribute("updateuser",ubean); 76. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 77. rd.forward(request, response); 78. } 79. } 80. catch(Exception e) 81. { 82. 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 respon
se) 100. throws ServletException, IOException { 101. processRequest(request, response); 102. } 103. 104. 105. protected void doPost(HttpServletRequest request, HttpServletResponse
response) 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 classview plainprint?
1. /* 2. * To change this template, choose Tools | Templates 3. * and open the template in the editor. 4. */ 5. 6. package database; 7. 8. import java.sql.Connection; 9. import java.sql.DriverManager; 10. import java.sql.PreparedStatement;
11. import java.sql.ResultSet; 12. import java.sql.SQLException; 13. import java.util.ArrayList; 14. import java.util.List; 15. import model.UserBean; 16. 17. /** 18. * 19. * @author Jagadeesh 20. */ 21. public class DBClass { 22. public Connection createConnection() throws ClassNotFoundException,SQLExceptio
n 23. { 24. Class.forName("com.mysql.jdbc.Driver"); 25. Connection connection = DriverManager.getConnection("jdbc:mysql://
localhost:3306/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, ClassNotFoundEx
ception 30. { 31. Connection con = createConnection(); 32. PreparedStatement pstmt = con.prepareStatement("insert into userdetails
values(?,?,?,?)"); 33. //set values to prepared statement object by getting values from bean o
bject 34. pstmt.setString(1,bb.getUserName()); 35. pstmt.setString(2,bb.getDateOfBirth()); 36. pstmt.setString(3,bb.getEmail()); 37. pstmt.setString(4,bb.getPhoneNo()); 38. int i = pstmt.executeUpdate(); 39. return i; 40. 41. } 42. public List getAlldetails()throws SQLException, ClassNotFoundException 43. { 44. 45. Connection con = createConnection(); 46. PreparedStatement pstmt = con.prepareStatement("select * from userdetai
ls"); 47. ResultSet rs = pstmt.executeQuery(); 48. List list = new ArrayList(); 49. while(rs.next()) 50. { 51. UserBean ubean = new UserBean(); 52. ubean.setUserName(rs.getString(1)); 53. ubean.setDateOfBirth(rs.getString(2)); 54. ubean.setEmail(rs.getString(3)); 55. ubean.setPhoneNo(rs.getString(4)); 56. list.add(ubean); 57. 58. } 59. return list; 60. } 61. public UserBean getDetails(String uname)throws SQLException, ClassNotFoundEx
ception 62. { 63. //here we will write code to get a single record from database 64. Connection con = createConnection();
65. PreparedStatement pstmt = con.prepareStatement("select * from userdetails where user_name=?");
66. pstmt.setString(1, uname); 67. ResultSet rs = pstmt.executeQuery(); 68. List list = new ArrayList(); 69. UserBean ubean = new UserBean(); 70. while(rs.next()) 71. { 72. 73. ubean.setUserName(rs.getString(1)); 74. ubean.setDateOfBirth(rs.getString(2)); 75. ubean.setEmail(rs.getString(3)); 76. ubean.setPhoneNo(rs.getString(4)); 77. } 78. ubean.setAction("update"); 79. return ubean; 80. } 81. /* public int UpateDetails(int uid)throws SQLException, ClassNotFoundExcept
ion 82. { 83. //here we will write code to update a record 84. }*/ 85. }
now display selected record in insertupdate.jsp pageview plainprint?
1. <%-- 2. Document : index 3. Author : Jagadeesh 4. --%> 5. 6. <%@page contentType="text/html" pageEncoding="UTF-8"%> 7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 8. "http://www.w3.org/TR/html4/loose.dtd"> 9. <%@page import="model.UserBean" %> 10. <% UserBean bean; 11. bean = new UserBean(); 12. //get request from servlet if data is invalid 13. if(request.getAttribute("error")!=null) 14. { 15. bean = (UserBean)request.getAttribute("error"); 16. } 17. if(request.getAttribute("updateuser")!=null) 18. { 19. bean = (UserBean)request.getAttribute("updateuser"); 20. } 21. %> 22. <html> 23. <head> 24. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 25. <title>JSP Page</title> 26. </head> 27. <body> 28. 29. <form method="post" action="ControllerServlet"> 30. <CENTER> 31. <TABLE border="0"width="600px"> 32. <TR> 33. <TD width="150px">Name:</TD> 34. <TD>
35. <INPUT TYPE="text" NAME="userName" value="<%=bean.getUserName()%>">
36. </TD> 37. <TD width="350px"> 38. <font color="red"><%=bean.getUserNameError()%> &nbs
p;</font> 39. </TD> 40. </TR> 41. <TR> 42. <TD width="150px">Date Of Birth:</TD> 43. <TD> 44. <INPUT TYPE="text" NAME="dateOfBirth" value="<
%=bean.getDateOfBirth()%>" <%=request.getAttribute("updateuser")!=null?"readonly":""%>>
45. </TD> 46. <TD> 47. <font color="red"><%=bean.getDateOfBirthError()%> <
/font> 48. </TD> 49. </TR> 50. <TR> 51. <TD width="150px">E-Mail</TD> 52. <TD> 53. <INPUT TYPE="text" NAME="email" value="<
%=bean.getEmail()%>"> 54. </TD> 55. <TD> 56. <font color="red"><%=bean.getEmailError()%> </
font> 57. </TD> 58. </TR> 59. <TR> 60. <TD width="150px">Phone no:</TD> 61. <TD> 62. <INPUT TYPE="text" NAME="phoneNo" value="<
%=bean.getPhoneNo()%>"> 63. </TD> 64. <TD> 65. <font color="red"><%=bean.getPhoneNoError()%> </
font> 66. </TD> 67. </TR> 68. <TR> 69. <TD colspan="2" align="center"> 70. <INPUT TYPE="submit" value="<%=bean.getAction()%>"
name="action"> 71. </TD> 72. <TD> 73. 74. </TD> 75. </TR> 76. </TABLE> 77. </CENTER> 78. </form> 79. </body> 80. </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 recordview plainprint?
1. import java.io.IOException; 2. import java.io.PrintWriter; 3. import javax.servlet.RequestDispatcher; 4. import javax.servlet.ServletException; 5. import javax.servlet.http.HttpServlet; 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. import model.UserBean; 9. import validation.ValidateForm; 10. import database.DBClass; 11. import java.util.List; 12. /** 13. * 14. * @author Jagadeesh 15. */ 16. public class ControllerServlet extends HttpServlet { 17. 18. //declare values to get form values from jsp page 19. String userName; 20. String dateOfBirth; 21. String email; 22. String phoneNo; 23. String action; 24. 25. UserBean bean = new UserBean(); 26. ValidateForm validateform = new ValidateForm(); 27. DBClass dbobject = new DBClass(); 28. protected void processRequest(HttpServletRequest request, HttpServletRespon
se response) 29. throws ServletException, IOException { 30. response.setContentType("text/html;charset=UTF-8"); 31. PrintWriter out = response.getWriter(); 32. try { 33. //get the values from jsp page 34. userName = request.getParameter("userName"); 35. dateOfBirth = request.getParameter("dateOfBirth"); 36. email = request.getParameter("email"); 37. phoneNo = request.getParameter("phoneNo"); 38. action = request.getParameter("action"); 39. if(action.equals("submit")) 40. { 41. //set values to bean.For this call below method 42. setValuesToBean(); 43. 44. //check all form values are valid or not. send bean object 45. UserBean checkedbean = validateform.validateData(bean); 46. if(!checkedbean.getIsValid()) 47. { 48. 49. //if data is invalid.set bean object in request and pass that r
equest to 50. //insertupdate.jsp using forward 51. checkedbean.setAction("submit"); 52. request.setAttribute("error",checkedbean); 53.
54. RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
55. rd.forward(request, response); 56. //now display errors in that jsp page 57. } 58. else 59. { 60. //using DBClass object call insertDetails method and pass bea
n object 61. dbobject.insertDetails(bean); 62. List list = dbobject.getAlldetails(); 63. request.setAttribute("list", list); 64. //forward to insertupdate page using requestdispatcher 65. RequestDispatcher rd= request.getRequestDispatcher
("view.jsp"); 66. //display a message to client.store message in request object 67. //forwarding to jsp 68. rd.forward(request, response); 69. 70. 71. } 72. } 73. if(action.equals("edit")) 74. { 75. //get userdetails of particular name 76. UserBean ubean = dbobject.getDetails(userName); 77. request.setAttribute("updateuser",ubean); 78. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 79. rd.forward(request, response); 80. } 81. if(action.equals("update")) 82. { 83. setValuesToBean(); 84. UserBean checkedbean = validateform.validateData(bean); 85. if(!checkedbean.getIsValid()) 86. { 87. 88. //if data is invalid.set bean object in request and pass that r
equest to 89. //insertupdate.jsp using forward 90. checkedbean.setAction("update"); 91. request.setAttribute("error",checkedbean); 92. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 93. rd.forward(request, response); 94. //now display errors in that jsp page 95. } 96. else 97. { 98. //using DBClass object call insertDetails method and pass bea
n object 99. dbobject.UpateDetails(bean,userName); 100. List list = dbobject.getAlldetails(); 101. request.setAttribute("list", list); 102. //forward to insertupdate page using requestdispatcher 103. RequestDispatcher rd= request.getRequestDispatcher
("view.jsp"); 104. //display a message to client.store message in request o
bject 105. //forwarding to jsp
106. rd.forward(request, response); 107. 108. 109. } 110. } 111. } 112. catch(Exception e) 113. { 114. out.println(e); 115. } 116. finally { 117. out.close(); 118. } 119. } 120. //this method is used to setvalues to bean 121. public void setValuesToBean() 122. { 123. bean.setUserName(userName); 124. bean.setDateOfBirth(dateOfBirth); 125. bean.setEmail(email); 126. bean.setPhoneNo(phoneNo); 127. 128. } 129. 130. 131. protected void doGet(HttpServletRequest request, HttpServletResponse
response) 132. throws ServletException, IOException { 133. processRequest(request, response); 134. } 135. 136. 137. protected void doPost(HttpServletRequest request, HttpServletResponse
response) 138. throws ServletException, IOException { 139. processRequest(request, response); 140. } 141. 142. 143. public String getServletInfo() { 144. return "Short description"; 145. } 146. 147. }
creating updatedetails method in dbclassview plainprint?
1. /* 2. * To change this template, choose Tools | Templates 3. * and open the template in the editor. 4. */ 5. 6. package database; 7. 8. import java.sql.Connection; 9. import java.sql.DriverManager; 10. import java.sql.PreparedStatement; 11. import java.sql.ResultSet; 12. import java.sql.SQLException; 13. import java.util.ArrayList;
14. import java.util.List; 15. import model.UserBean; 16. 17. /** 18. * 19. * @author Jagadeesh 20. */ 21. public class DBClass { 22. public Connection createConnection() throws ClassNotFoundException,SQLExceptio
n 23. { 24. Class.forName("com.mysql.jdbc.Driver"); 25. Connection connection = DriverManager.getConnection("jdbc:mysql://
localhost:3306/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, ClassNotFoundEx
ception 30. { 31. Connection con = createConnection(); 32. PreparedStatement pstmt = con.prepareStatement("insert into userdetails
values(?,?,?,?)"); 33. //set values to prepared statement object by getting values from bean o
bject 34. pstmt.setString(1,bb.getUserName()); 35. pstmt.setString(2,bb.getDateOfBirth()); 36. pstmt.setString(3,bb.getEmail()); 37. pstmt.setString(4,bb.getPhoneNo()); 38. int i = pstmt.executeUpdate(); 39. return i; 40. 41. } 42. public List getAlldetails()throws SQLException, ClassNotFoundException 43. { 44. 45. Connection con = createConnection(); 46. PreparedStatement pstmt = con.prepareStatement("select * from userdetai
ls"); 47. ResultSet rs = pstmt.executeQuery(); 48. List list = new ArrayList(); 49. while(rs.next()) 50. { 51. UserBean ubean = new UserBean(); 52. ubean.setUserName(rs.getString(1)); 53. ubean.setDateOfBirth(rs.getString(2)); 54. ubean.setEmail(rs.getString(3)); 55. ubean.setPhoneNo(rs.getString(4)); 56. list.add(ubean); 57. 58. } 59. return list; 60. } 61. public UserBean getDetails(String uname)throws SQLException, ClassNotFoundEx
ception 62. { 63. //here we will write code to get a single record from database 64. Connection con = createConnection(); 65. PreparedStatement pstmt = con.prepareStatement("select * from userdetai
ls where user_name=?"); 66. pstmt.setString(1, uname);
67. ResultSet rs = pstmt.executeQuery(); 68. List list = new ArrayList(); 69. UserBean ubean = new UserBean(); 70. while(rs.next()) 71. { 72. 73. ubean.setUserName(rs.getString(1)); 74. ubean.setDateOfBirth(rs.getString(2)); 75. ubean.setEmail(rs.getString(3)); 76. ubean.setPhoneNo(rs.getString(4)); 77. } 78. ubean.setAction("update"); 79. return ubean; 80. } 81. public void UpateDetails(UserBean ubean, String name)throws SQLException,
ClassNotFoundException 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 o
bject 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 pagefor this we will create deleteDetails method in dbclassview plainprint?
1. import java.io.IOException; 2. import java.io.PrintWriter; 3. import javax.servlet.RequestDispatcher; 4. import javax.servlet.ServletException; 5. import javax.servlet.http.HttpServlet; 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. import model.UserBean; 9. import validation.ValidateForm; 10. import database.DBClass; 11. import java.util.List; 12. /** 13. * 14. * @author Jagadeesh 15. */ 16. public class ControllerServlet extends HttpServlet { 17. 18. //declare values to get form values from jsp page 19. String userName; 20. String dateOfBirth; 21. String email; 22. String phoneNo; 23. String action; 24. 25. UserBean bean = new UserBean(); 26. ValidateForm validateform = new ValidateForm(); 27. DBClass dbobject = new DBClass(); 28. protected void processRequest(HttpServletRequest request, HttpServletRespon
se response) 29. throws ServletException, IOException { 30. response.setContentType("text/html;charset=UTF-8"); 31. PrintWriter out = response.getWriter(); 32. try { 33. //get the values from jsp page 34. userName = request.getParameter("userName"); 35. dateOfBirth = request.getParameter("dateOfBirth"); 36. email = request.getParameter("email"); 37. phoneNo = request.getParameter("phoneNo"); 38. action = request.getParameter("action"); 39. if(action.equals("submit")) 40. { 41. //set values to bean.For this call below method 42. setValuesToBean(); 43. 44. //check all form values are valid or not. send bean object 45. UserBean checkedbean = validateform.validateData(bean); 46. if(!checkedbean.getIsValid()) 47. { 48. 49. //if data is invalid.set bean object in request and pass that r
equest to 50. //insertupdate.jsp using forward 51. checkedbean.setAction("submit"); 52. request.setAttribute("error",checkedbean); 53.
54. RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
55. rd.forward(request, response); 56. //now display errors in that jsp page 57. } 58. else 59. { 60. //using DBClass object call insertDetails method and pass bea
n object 61. dbobject.insertDetails(bean); 62. List list = dbobject.getAlldetails(); 63. request.setAttribute("list", list); 64. //forward to insertupdate page using requestdispatcher 65. RequestDispatcher rd= request.getRequestDispatcher
("view.jsp"); 66. //display a message to client.store message in request object 67. //forwarding to jsp 68. rd.forward(request, response); 69. 70. 71. } 72. } 73. if(action.equals("edit")) 74. { 75. //get userdetails of particular name 76. UserBean ubean = dbobject.getDetails(userName); 77. request.setAttribute("updateuser",ubean); 78. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 79. rd.forward(request, response); 80. } 81. if(action.equals("update")) 82. { 83. setValuesToBean(); 84. UserBean checkedbean = validateform.validateData(bean); 85. if(!checkedbean.getIsValid()) 86. { 87. 88. //if data is invalid.set bean object in request and pass that r
equest to 89. //insertupdate.jsp using forward 90. checkedbean.setAction("update"); 91. request.setAttribute("error",checkedbean); 92. RequestDispatcher rd = request.getRequestDispatcher
("insertupdate.jsp"); 93. rd.forward(request, response); 94. //now display errors in that jsp page 95. } 96. else 97. { 98. //using DBClass object call insertDetails method and pass bea
n object 99. dbobject.UpateDetails(bean,userName); 100. List list = dbobject.getAlldetails(); 101. request.setAttribute("list", list); 102. //forward to insertupdate page using requestdispatcher 103. RequestDispatcher rd= request.getRequestDispatcher
("view.jsp"); 104. //display a message to client.store message in request o
bject 105. //forwarding to jsp
106. rd.forward(request, response); 107. 108. 109. } 110. 111. } 112. if(action.equals("delete")) 113. { 114. //delete userdetails of particular name 115. dbobject.deleteDetails(userName); 116. RequestDispatcher rd = request.getRequestDispatcher
("view.jsp"); 117. rd.forward(request, response); 118. } 119. } 120. catch(Exception e) 121. { 122. out.println(e); 123. } 124. finally { 125. out.close(); 126. } 127. } 128. //this method is used to setvalues to bean 129. public void setValuesToBean() 130. { 131. bean.setUserName(userName); 132. bean.setDateOfBirth(dateOfBirth); 133. bean.setEmail(email); 134. bean.setPhoneNo(phoneNo); 135. 136. } 137. 138. 139. protected void doGet(HttpServletRequest request, HttpServletResponse
response) 140. throws ServletException, IOException { 141. processRequest(request, response); 142. } 143. 144. 145. protected void doPost(HttpServletRequest request, HttpServletResponse
response) 146. throws ServletException, IOException { 147. processRequest(request, response); 148. } 149. 150. 151. public String getServletInfo() { 152. return "Short description"; 153. } 154. 155. }
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