Java homework please help! :(

Reply

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

Java homework please help! :(

 
0
  #1
Dec 19th, 2007
I coded a script, Currently I can add and delete items to the database but I cannot edit the data. Here's my 3 paged script :

Christmasswishlist.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

/**
* Servlet implementation class for Servlet: ChristmasWishlist
*
*/
public class ChristmasWishlist extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet
{
static final long serialVersionUID = 1L;

/*
* Connect to the database
*/

/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public ChristmasWishlist()
{
super();
}

/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
process(request, response);
}

/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
process(request, response);
}

protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><title>List of All Your Wishes</title><body>");

//check if you are getting the right values, comment out once the program is working
//line below will work only for java 5 up
java.util.Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String gift = e.nextElement().toString();
out.println("<br>" + gift + " - " + request.getParameter(gift));
}

if (request.getParameter("submit") != null)
{
if (!request.getParameter("submit").isEmpty())
{
if (request.getParameter("submit").equals("Add"))
{
if (!request.getParameter("gift").isEmpty())
{
out.println(addChristmasWishlist(request
.getParameter("gift")));
}
else
{
out.println("<p>Nothing to do");
}
}

else if(request.getParameter("submit").equals("Delete"))
{
delChristmasWishlist(Integer.parseInt(request.getParameter("id")));
}
else if(request.getParameter("submit").equals("Delete"))
{
out.println("<p>Nothing to do");
}
}

} else
{
out.println("<p>empty");
}
out.println("<p><a href=wish.html>Add again</a>");

ResultSet rst = null;
try
{
rst = viewChristmasWishlist();
int count = 1;
out.println("<table><tr><th colspan=2>ChristmasWishlist</th></tr>");
while(rst.next())
{
out.println("<tr><td><b>"+count+".</b></td><td>"+rst.getString("gift")+"</td>" +
"<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Edit>Edit</a.</td>"+
"<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Delete>Delete</a.</td></tr>");
count++;
}
out.println("</table>");
}
catch(Exception e2)
{

}

out.println("</body></html>");

}

public String addChristmasWishlist(String wish)
{

try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "insert into wishes set gift='" + wish.replaceAll("\'", "\\\'") + "'";
if (stmt.execute(sql))
{
msg = "Insert failed";
}
else
{
msg = "Added successfully";
}
con.logOff();
return msg;

}
catch (SQLException e)
{

return "Add failed. SQL error " + e.getMessage();
}
catch (java.lang.NullPointerException ne)
{

return "<?p>Null error: " + ne.getMessage();
}

}


public String delChristmasWishlist(int id)
{

try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "delete from wishes where id="+id;
if (stmt.execute(sql))
{
msg = "Delete failed";
} else
{
msg = "Delete successfully";
}
con.logOff();
return msg;

} catch (SQLException e)
{

return "Add failed. SQL error " + e.getMessage();
} catch (java.lang.NullPointerException ne)
{

return "<p>Null error: " + ne.getMessage();
}



}

public String editChristmasWishlist(String wish)
{

try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "update from wishes where gift="+wish;
if (stmt.execute(sql))
{
msg = "update failed";
} else
{
msg = "update successfully";
}
con.logOff();
return msg;

} catch (SQLException e)
{

return "Add failed. SQL error " + e.getMessage();
} catch (java.lang.NullPointerException ne)
{

return "<p>Null error: " + ne.getMessage();
}



}

public ResultSet viewChristmasWishlist()
{

ResultSet rst = null;
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";

String sql = "select * from wishes";
rst = stmt.executeQuery(sql);


} catch (SQLException e)
{


} catch (java.lang.NullPointerException ne)
{


}
return rst;
}



Connectionmanager.java

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionManager {

protected Connection con;
protected String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String database = "WishList";
String user = "root";
String password = "";

/** Creates a new instance of ConnectionManager */
public ConnectionManager() {

}
public Connection logOn(){
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+database,user,password);

}
catch(Exception e){
System.out.print(e.getMessage());
}
return con;

}

public void logOff(){
try {
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}

Wish.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Christmas Wishes</title>
</head>
<body>
<h3>Place Your Christmas Wishes Here</h3>
<form action="ChristmasWishlist" method=post>
<table>
<tr><th>Wish:</th><td><input type=text name=gift size=100></td></tr>
<tr><th>Amount:</th><td><input type=text name=amount size=10></td></tr>
<tr><th colspan=2><input type=submit name=submit value="Add"></th></tr>
</table>
</form>
</body>
</html>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Java homework please help! :(

 
0
  #2
Dec 19th, 2007
  • No one is going to look at that without [code][/code] tags
  • And you need to pinpoint a specific issue you are having, not just post your entire code

If anyone else wants to look...

  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. }
  100. else
  101. {
  102. out.println ( "<p>empty" );
  103. }
  104. out.println ( "<p><a href=wish.html>Add again</a>" );
  105.  
  106. ResultSet rst = null;
  107. try
  108. {
  109. rst = viewChristmasWishlist();
  110. int count = 1;
  111. out.println ( "<table><tr><th colspan=2>ChristmasWishlist</th></tr>" );
  112. while ( rst.next() )
  113. {
  114. out.println ( "<tr><td><b>" + count + ".</b></td><td>" + rst.getString ( "gift" ) + "</td>" +
  115. "<td><a href=ChristmasWishlist?id=" + rst.getInt ( "id" ) + "&submit=Edit>Edit</a.</td>" +
  116. "<td><a href=ChristmasWishlist?id=" + rst.getInt ( "id" ) + "&submit=Delete>Delete</a.</td></tr>" );
  117. count++;
  118. }
  119. out.println ( "</table>" );
  120. }
  121. catch ( Exception e2 )
  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. public String delChristmasWishlist ( int id )
  163. {
  164.  
  165. try
  166. {
  167. ConnectionManager con = new ConnectionManager();
  168. Statement stmt = con.logOn().createStatement();
  169. String msg = "";
  170. String sql = "delete from wishes where id=" + id;
  171. if ( stmt.execute ( sql ) )
  172. {
  173. msg = "Delete failed";
  174. }
  175. else
  176. {
  177. msg = "Delete successfully";
  178. }
  179. con.logOff();
  180. return msg;
  181.  
  182. }
  183. catch ( SQLException e )
  184. {
  185.  
  186. return "Add failed. SQL error " + e.getMessage();
  187. }
  188. catch ( java.lang.NullPointerException ne )
  189. {
  190.  
  191. return "<p>Null error: " + ne.getMessage();
  192. }
  193.  
  194. }
  195.  
  196. public String editChristmasWishlist ( String wish )
  197. {
  198.  
  199. try
  200. {
  201. ConnectionManager con = new ConnectionManager();
  202. Statement stmt = con.logOn().createStatement();
  203. String msg = "";
  204. String sql = "update from wishes where gift=" + wish;
  205. if ( stmt.execute ( sql ) )
  206. {
  207. msg = "update failed";
  208. }
  209. else
  210. {
  211. msg = "update successfully";
  212. }
  213. con.logOff();
  214. return msg;
  215.  
  216. }
  217. catch ( SQLException e )
  218. {
  219.  
  220. return "Add failed. SQL error " + e.getMessage();
  221. }
  222. catch ( java.lang.NullPointerException ne )
  223. {
  224.  
  225. return "<p>Null error: " + ne.getMessage();
  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. catch ( SQLException e )
  244. {
  245.  
  246. }
  247. catch ( java.lang.NullPointerException ne )
  248. {
  249.  
  250. }
  251. return rst;
  252. }

Connectionmanager.java

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

Wish.html

  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>
Last edited by iamthwee; Dec 19th, 2007 at 4:07 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,824
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Java homework please help! :(

 
0
  #3
Dec 20th, 2007
Are you being able to execute "update from wishes where gift=<giftid>" via command prompt ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: patmagpantay is an unknown quantity at this point 
Solved Threads: 0
patmagpantay patmagpantay is offline Offline
Newbie Poster

Re: Java homework please help! :(

 
0
  #4
Dec 20th, 2007
unfortunately i cant execute it through the command prompt
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC