I need help with my java servlet homework(edit)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 9
Reputation: jellyfish888 is an unknown quantity at this point 
Solved Threads: 0
jellyfish888 jellyfish888 is offline Offline
Newbie Poster

I need help with my java servlet homework(edit)

 
0
  #1
Dec 24th, 2007
I have a homework in which our teacher asked us to write a code in which the user can add, delete and edit his wishlist...I can only do add and delete...I am sure that there is a lot of problem for edit so please help me to make it work...I am using database for this...

This is my html code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Christmas Wishes</title>
  6. </head>
  7. <body>
  8. <h3>Place Your Christmas Wishes Here</h3>
  9. <form action="ChristmasWishlist" method=post>
  10. <table>
  11. <tr><th>Wish:</th><td><input type=text name=gift size=100></td></tr>
  12. <tr><th>Amount:</th><td><input type=text name=amount size=10></td></tr>
  13. <tr><th colspan=2><input type=submit name=submit value="Add"></th></tr>
  14. </table>
  15. </form>
  16. </body>
  17. </html>

This is my servlet:
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3.  
  4. public class ConnectionManager {
  5.  
  6. protected Connection con;
  7. protected String driver = "com.mysql.jdbc.Driver";
  8. String url = "jdbc:mysql://localhost:3306/";
  9. String database = "WishList";
  10. String user = "root";
  11. String password = "";
  12.  
  13. /** Creates a new instance of ConnectionManager */
  14. public ConnectionManager() {
  15.  
  16. }
  17. public Connection logOn(){
  18. try {
  19. Class.forName(driver).newInstance();
  20. con = DriverManager.getConnection(url+database,user,password);
  21.  
  22. }
  23. catch(Exception e){
  24. System.out.print(e.getMessage());
  25. }
  26. return con;
  27.  
  28. }
  29.  
  30. public void logOff(){
  31. try {
  32. con.close();
  33. }
  34. catch(Exception e){
  35. e.printStackTrace();
  36. }
  37. }
  38. }

This is my other servlet:
  1. import java.io.IOException;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import java.sql.*;
  6.  
  7. /**
  8.  * Servlet implementation class for Servlet: ChristmasWishlist
  9.  *
  10.  */
  11. public class ChristmasWishlist extends javax.servlet.http.HttpServlet implements
  12. javax.servlet.Servlet
  13. {
  14. static final long serialVersionUID = 1L;
  15.  
  16. /*
  17. * Connect to the database
  18. */
  19.  
  20. /*
  21. * (non-Java-doc)
  22. *
  23. * @see javax.servlet.http.HttpServlet#HttpServlet()
  24. */
  25. public ChristmasWishlist()
  26. {
  27. super();
  28. }
  29.  
  30. /*
  31. * (non-Java-doc)
  32. *
  33. * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
  34. * HttpServletResponse response)
  35. */
  36. protected void doGet(HttpServletRequest request,
  37. HttpServletResponse response) throws ServletException, IOException
  38. {
  39. // TODO Auto-generated method stub
  40. process(request, response);
  41. }
  42.  
  43. /*
  44. * (non-Java-doc)
  45. *
  46. * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
  47. * HttpServletResponse response)
  48. */
  49. protected void doPost(HttpServletRequest request,
  50. HttpServletResponse response) throws ServletException, IOException
  51. {
  52. // TODO Auto-generated method stub
  53. process(request, response);
  54. }
  55.  
  56. protected void process(HttpServletRequest request,
  57. HttpServletResponse response) throws ServletException, IOException
  58. {
  59. response.setContentType("text/html");
  60. java.io.PrintWriter out = response.getWriter();
  61. out.println("<html><title>List of All Your Wishes</title><body>");
  62.  
  63. //check if you are getting the right values, comment out once the program is working
  64. //line below will work only for java 5 up
  65. java.util.Enumeration e = request.getParameterNames();
  66. while (e.hasMoreElements())
  67. {
  68. String gift = e.nextElement().toString();
  69. out.println("<br>" + gift + " - " + request.getParameter(gift));
  70. }
  71.  
  72. if (request.getParameter("submit") != null)
  73. {
  74. if (!request.getParameter("submit").isEmpty())
  75. {
  76. if (request.getParameter("submit").equals("Add"))
  77. {
  78. if (!request.getParameter("gift").isEmpty())
  79. {
  80. out.println(addChristmasWishlist(request
  81. .getParameter("gift")));
  82. }
  83. else
  84. {
  85. out.println("<p>Nothing to do");
  86. }
  87. }
  88.  
  89. else if(request.getParameter("submit").equals("Delete"))
  90. {
  91. delChristmasWishlist(Integer.parseInt(request.getParameter("id")));
  92. }
  93. else if(request.getParameter("submit").equals("Delete"))
  94. {
  95. out.println("<p>Nothing to do");
  96. }
  97. }
  98.  
  99. } else
  100. {
  101. out.println("<p>empty");
  102. }
  103. out.println("<p><a href=wish.html>Add again</a>");
  104.  
  105. ResultSet rst = null;
  106. try
  107. {
  108. rst = viewChristmasWishlist();
  109. int count = 1;
  110. out.println("<table><tr><th colspan=2>ChristmasWishlist</th></tr>");
  111. while(rst.next())
  112. {
  113. out.println("<tr><td><b>"+count+".</b></td><td>"+rst.getString("gift")+"</td>" +
  114. "<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Edit>Edit</a.</td>"+
  115. "<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Delete>Delete</a.</td></tr>");
  116. count++;
  117. }
  118. out.println("</table>");
  119. }
  120. catch(Exception e2)
  121. {
  122.  
  123. }
  124.  
  125. out.println("</body></html>");
  126.  
  127. }
  128.  
  129. public String addChristmasWishlist(String wish)
  130. {
  131.  
  132. try
  133. {
  134. ConnectionManager con = new ConnectionManager();
  135. Statement stmt = con.logOn().createStatement();
  136. String msg = "";
  137. String sql = "insert into wishes set gift='" + wish.replaceAll("\'", "\\\'") + "'";
  138. if (stmt.execute(sql))
  139. {
  140. msg = "Insert failed";
  141. }
  142. else
  143. {
  144. msg = "Added successfully";
  145. }
  146. con.logOff();
  147. return msg;
  148.  
  149. }
  150. catch (SQLException e)
  151. {
  152.  
  153. return "Add failed. SQL error " + e.getMessage();
  154. }
  155. catch (java.lang.NullPointerException ne)
  156. {
  157.  
  158. return "<?p>Null error: " + ne.getMessage();
  159. }
  160.  
  161. }
  162.  
  163.  
  164. public String delChristmasWishlist(int id)
  165. {
  166.  
  167. try
  168. {
  169. ConnectionManager con = new ConnectionManager();
  170. Statement stmt = con.logOn().createStatement();
  171. String msg = "";
  172. String sql = "delete from wishes where id="+id;
  173. if (stmt.execute(sql))
  174. {
  175. msg = "Delete failed";
  176. } else
  177. {
  178. msg = "Delete successfully";
  179. }
  180. con.logOff();
  181. return msg;
  182.  
  183. } catch (SQLException e)
  184. {
  185.  
  186. return "Add failed. SQL error " + e.getMessage();
  187. } catch (java.lang.NullPointerException ne)
  188. {
  189.  
  190. return "<p>Null error: " + ne.getMessage();
  191. }
  192.  
  193.  
  194.  
  195. }
  196.  
  197. public String editChristmasWishlist(String wish)
  198. {
  199.  
  200. try
  201. {
  202. ConnectionManager con = new ConnectionManager();
  203. Statement stmt = con.logOn().createStatement();
  204. String msg = "";
  205. String sql = "update from wishes where gift="+wish;
  206. if (stmt.execute(sql))
  207. {
  208. msg = "update failed";
  209. } else
  210. {
  211. msg = "update successfully";
  212. }
  213. con.logOff();
  214. return msg;
  215.  
  216. } catch (SQLException e)
  217. {
  218.  
  219. return "Add failed. SQL error " + e.getMessage();
  220. } catch (java.lang.NullPointerException ne)
  221. {
  222.  
  223. return "<p>Null error: " + ne.getMessage();
  224. }
  225.  
  226.  
  227.  
  228. }
  229.  
  230. public ResultSet viewChristmasWishlist()
  231. {
  232.  
  233. ResultSet rst = null;
  234. try
  235. {
  236. ConnectionManager con = new ConnectionManager();
  237. Statement stmt = con.logOn().createStatement();
  238. String msg = "";
  239.  
  240. String sql = "select * from wishes";
  241. rst = stmt.executeQuery(sql);
  242.  
  243.  
  244. } catch (SQLException e)
  245. {
  246.  
  247.  
  248. } catch (java.lang.NullPointerException ne)
  249. {
  250.  
  251.  
  252. }
  253. return rst;
  254. }
  255.  
  256. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 9
Reputation: jellyfish888 is an unknown quantity at this point 
Solved Threads: 0
jellyfish888 jellyfish888 is offline Offline
Newbie Poster

Re: I need help with my java servlet homework(edit)

 
0
  #2
Dec 24th, 2007
please help me with this...you can just concentrate on edit since I was able to do add and delete...please and thank you...

  1. public String editChristmasWishlist(String wish)
  2. {
  3.  
  4. try
  5. {
  6. ConnectionManager con = new ConnectionManager();
  7. Statement stmt = con.logOn().createStatement();
  8. String msg = "";
  9. String sql = "update from wishes where gift="+wish;
  10. if (stmt.execute(sql))
  11. {
  12. msg = "update failed";
  13. } else
  14. {
  15. msg = "update successfully";
  16. }
  17. con.logOff();
  18. return msg;
  19.  
  20. } catch (SQLException e)
  21. {
  22.  
  23. return "Add failed. SQL error " + e.getMessage();
  24. } catch (java.lang.NullPointerException ne)
  25. {
  26.  
  27. return "<p>Null error: " + ne.getMessage();
  28. }
  29.  
  30.  
  31.  
  32. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 473
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: I need help with my java servlet homework(edit)

 
0
  #3
Dec 24th, 2007
We have no way of knowing how your interface looks / how your table structure looks like. Next time try posting a more detailed explanation of how your application looks like.

There are some visible problems with your code:
  • Consider using PreparedStatement instead of normal Statement to get around the problem of SQL Injection and complicated quoting / unquoting.
  • NullPointerException is a runtime exception which can be very well avoided with a simple check. The very fact that you are catching it shows there is something wrong with your design.
  • And the most important of all, your SQL statement is hosed! The syntax of your update SQL statement is incorrect.
    UPDATE table_name SET column_name = value WHERE column_name = that_thing;
  • The logic seems to be incorrect. Do you plan on updating the wishlist of all those people who have the same wish? At least that is what it seems to me since you seem to be updating on 'wish' and not on the unique 'id'. (if you have one)
At least skim through the basic SQL and JDBC tutorial before you consider writing a web application. Start here and here.
Last edited by ~s.o.s~; Dec 24th, 2007 at 8:35 am.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC