Update&delete
Upcoming SlideShare
Loading in...5
×
 

Update&delete

on

  • 195 views

 

Statistics

Views

Total Views
195
Views on SlideShare
195
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Update&delete Update&delete Presentation Transcript

  • SQL UPDATE Table) Record 1 Where Update Database (MySQL) Code UPDATE [Table-Name] SET Column1='Value1',Column2='Value2',... WHERE clause UPDATE SET 1=’ 1′, 2=’ 2′ WHERE ’ ’ UPDATE tb_department SET dep_name=’testedit’, dep_sname=’ ’ WHEREdep_id =’ 2′
  • SQL UPDATE Table) Record Record Where delete Database (MySQL) Code DELETE FROM [Table-Name] WHERE clause DELETE DELETE FROM TABLE_NAME = WHERE =
  • DELETE FROM membersbook WHEREmember_id=”+ID+” ”; JSP edit 1. WebContent--> New --> JSP file edit.jsp
  • 2. edit.jsp
  • EditServlet.java 1. Package com.controller--> New -- > Servlet EditServlet 2. EditServlet. java Method doPost Method doget
  • UpdateServlet.java 1. UpdateServlet.java
  • edit.jsp DeleteServet.java 1. DeleteServet. java Method doPost
  • MemberDAO.java 1. MemberDAO.java Method Method Method MemberDAO.java 2. Method MemberDAO.java
  • 3. Method MemberDAO.java
  • Code Class edit <body> <% MembersBean mem = (MembersBean)request.getAttribute("MemberData"); %> <form action="UpdateServlet" method="post"> <input type="hidden" value="<%=mem.getMember_id()%>"name ="memberID"/> <table> <tr>
  • <td align="right"> </td> <td align="center" bgcolor="green"><input type="text" name="name" value ="<%=mem.getMember_firstname()%>"></td> </tr> <tr> <td align="right"> </td> <td align="center" bgcolor="green"><input type="text" name="lastname" value ="<%=mem.getMember_lastname()%>"></td> </tr> <tr> <td align="right"> </td> <td align="center" bgcolor="green"><textarea rows="5" cols="10" name="address" ><%=mem.getMember_address()%></textarea></td > </tr> <tr> <td align="right"> </td>
  • <td align="center" bgcolor="green"><input type="text" name="phone" value ="<%=mem.getMember_number()%>"></td> </tr> <tr> <td colspan="2" align="center"><input value=" type="submit"><input value=" type="reset"></td> </tr> </table> </form> </body> Code Class EditServlet protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /**
  • * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String memberID = request.getParameter("memberID"); MemberDAO md = new MemberDAO(); String page = "edit.jsp"; request.setAttribute("MemberData",md.getMem ber(memberID)); RequestDispatcher dispatcher = request.getRequestDispatcher(page); if(dispatcher != null){ dispatcher.forward(request, response); //forward request, response
  • System.out.println("send complete"); // send complete } // } } Code Class UpdateServlet protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); String id = request.getParameter("memberID"); String fname = request.getParameter("name") ; String lname = request.getParameter("lastname") ; String addr = request.getParameter("address") ; String tel = request.getParameter("phone"); String page = "Search.jsp"; // Profile.jsp
  • MemberDAO md = new MemberDAO(); MembersBean mem = new MembersBean(); mem.setMember_id(Integer.parseInt(id)); mem.setMember_firstname(fname); mem.setMember_lastname(lname); mem.setMember_address(addr); mem.setMember_number(tel); md.UpdateMembers(mem); RequestDispatcher dispatcher = request.getRequestDispatcher(page); if(dispatcher != null){ dispatcher.forward(request, response); //forward request, response System.out.println("send complete"); // send complete } // } Code Class DeleteServet
  • protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String id = request.getParameter("memberID"); String page = "Search.jsp"; // Search.jsp MemberDAO md = new MemberDAO(); MembersBean mem = new MembersBean(); mem.setMember_id(Integer.parseInt(id));
  • md.DeleteMembers(id); RequestDispatcher dispatcher = request.getRequestDispatcher(page); if(dispatcher != null){ dispatcher.forward(request, response); //forward request, response System.out.println("send complete"); // send complete } // } Code Class MemberDAO public static MembersBean getMember(String ID){ MembersBean mem = new MembersBean(); String sql = "SELECT * FROM membersbook WHERE member_id="+ID+""; //SELECT FROM members try {
  • dbc.createConnection(); // stmt = dbc.getStatement(); // getStatement() stmt rs = stmt.executeQuery(sql); // rs while (rs.next()) { mem = new MembersBean(); mem.setMember_id(rs.getInt("member_id")); mem.setMember_username(rs.getString("member_u sername")); mem.setMember_password(rs.getString("member_pa ssword")); mem.setMember_firstname(rs.getString("member_fir stname")); mem.setMember_lastname(rs.getString("member_las tname"));
  • mem.setMember_sex(rs.getString("member_sex")); mem.setMember_address(rs.getString("member_add ress")); mem.setMember_county(rs.getString("member_count y")); mem.setMember_number(rs.getString("member_num ber")); mem.setMember_email(rs.getString("member_email" )); mem.setMember_image(rs.getString("member_imag e")); } } catch (Exception e) { e.printStackTrace(); // error }
  • return mem; } ------------------------------- Method Update---------- ---------------- public static boolean UpdateMembers(MembersBean mem){ String sql = "UPDATE membersbook SET member_firstname='" +mem.getMember_firstname()+"',member_lastname= '" +mem.getMember_lastname()+"',member_address='" +mem.getMember_address()+"',member_number='" +mem.getMember_number()+"'WHERE member_id=" +mem.getMember_id(); try { dbc.createConnection(); //
  • stmt = dbc.getStatement(); // getStatement() stmt stmt.executeUpdate(sql); // rs } catch (Exception e) { e.printStackTrace(); // error } return true; } ------------------------------- Method Delete----------- --------------- public static boolean DeleteMembers(String ID){ String sql = "DELETE FROM membersbook WHERE member_id="+ID+""; try{ dbc.createConnection(); // stmt = dbc.getStatement(); // getStatement() stmt
  • stmt.executeUpdate(sql); // rs } catch (Exception e) { e.printStackTrace(); // error } return true; } -http://www.code- father.com/%E0%B8%84%E0%B8%B3%E0%B8%AA%E0%B8%B1%E0%B9%88%E0%B 8%87-sql-Select-from-Where-,Like-,Order-By- %E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%88%E0%B8%B3%E0%B9%80%E0%B 8%9B%E0%B9%87%E0%B8%99%E0%B8%95%E0%B9%89%E0%B8%AD%E0%B8%87% E0%B9%83%E0%B8%8A%E0%B9%89%E0%B9%81%E0%B8%A5%E0%B8%B0%E0%B8 %95%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B9%83%E0%B8%8A%E0%B9%89%E 0%B8%9A%E0%B9%88%E0%B8%AD%E0%B8%A2-%E0%B9%86_00022.html -http://www.select2web.com/sql/sql-delete-commmand.html -http://www.thaicreate.com/tutorial/sql-delete.html
  • -http://blog.tnomagic.com/2011/07/update-mysql/ -http://www.thaicreate.com/tutorial/sql-update.html