SQL UPDATE
Table)
Record
1 Where
Update Database (MySQL)
Code
UPDATE [Table-Name] SET
Column1='Value1',Column2='Value2',.....
SQL UPDATE
Table)
Record
Record Where
delete Database (MySQL)
Code
DELETE FROM [Table-Name] WHERE clause
DELETE
DELETE FRO...
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="UpdateServle...
<td align="right"> </td>
<td align="center" bgcolor="green"><input type="text"
name="name" value
="<%=mem.getMember_firstn...
<td align="center" bgcolor="green"><input type="text"
name="phone" value
="<%=mem.getMember_number()%>"></td>
</tr>
<tr>
<...
* @see
HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRe...
System.out.println("send complete");
// send complete
} //
}
}
Code Class UpdateServlet
protected void doPost(HttpServletR...
MemberDAO md = new MemberDAO();
MembersBean mem = new MembersBean();
mem.setMember_id(Integer.parseInt(id));
mem.setMember...
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// T...
md.DeleteMembers(id);
RequestDispatcher dispatcher =
request.getRequestDispatcher(page);
if(dispatcher != null){
dispatche...
dbc.createConnection(); //
stmt = dbc.getStatement();
// getStatement() stmt
rs = stmt.executeQuery(sql);
// rs
while (rs....
mem.setMember_sex(rs.getString("member_sex"));
mem.setMember_address(rs.getString("member_add
ress"));
mem.setMember_count...
return mem;
}
------------------------------- Method Update----------
----------------
public static boolean UpdateMembers...
stmt = dbc.getStatement();
// getStatement() stmt
stmt.executeUpdate(sql);
// rs
} catch (Exception e) {
e.printStackTrace...
stmt.executeUpdate(sql);
// rs
} catch (Exception e) {
e.printStackTrace(); // error
}
return true;
}
-http://www.code-
fa...
-http://blog.tnomagic.com/2011/07/update-mysql/
-http://www.thaicreate.com/tutorial/sql-update.html
Upcoming SlideShare
Loading in …5
×

Update&delete

247 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
247
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Update&delete

  1. 1. 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′
  2. 2. SQL UPDATE Table) Record Record Where delete Database (MySQL) Code DELETE FROM [Table-Name] WHERE clause DELETE DELETE FROM TABLE_NAME = WHERE =
  3. 3. DELETE FROM membersbook WHEREmember_id=”+ID+” ”; JSP edit 1. WebContent--> New --> JSP file edit.jsp
  4. 4. 2. edit.jsp
  5. 5. EditServlet.java 1. Package com.controller--> New -- > Servlet EditServlet 2. EditServlet. java Method doPost Method doget
  6. 6. UpdateServlet.java 1. UpdateServlet.java
  7. 7. edit.jsp DeleteServet.java 1. DeleteServet. java Method doPost
  8. 8. MemberDAO.java 1. MemberDAO.java Method Method Method MemberDAO.java 2. Method MemberDAO.java
  9. 9. 3. Method MemberDAO.java
  10. 10. 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>
  11. 11. <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>
  12. 12. <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); } /**
  13. 13. * @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
  14. 14. 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
  15. 15. 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
  16. 16. 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));
  17. 17. 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 {
  18. 18. 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"));
  19. 19. 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 }
  20. 20. 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(); //
  21. 21. 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
  22. 22. 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
  23. 23. -http://blog.tnomagic.com/2011/07/update-mysql/ -http://www.thaicreate.com/tutorial/sql-update.html

×