Results 1 to 4 of 4
  1. #1
    jellyfish888 is offline Member
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default I need help with my java servlet homework(add,edit and delete)

    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:
    Java Code:
    <!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>
    This is my servlet:
    Java Code:
    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();
            }
        }
    }
    This is my other servlet:
    Java Code:
    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;
    	}
    
    }

  2. #2
    jellyfish888 is offline Member
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default

    please help me with this...you can just concentrate on edit since I was able to do add and delete...please and thank you...

  3. #3
    jellyfish888 is offline Member
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default This is the dit part which I think have a lot of problem...

    Java Code:
    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();
    		}
    		
    		
    
    	}

    Please help me make this code work...

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default

    What is the actual problem you get from your code? can you tell us?

    What I am guessing right now is could be you are missing a quote in your sql update command. Shouldn't it be something like:

    Java Code:
    "update from wishes where gift='" + wish + "'";

Similar Threads

  1. Can I delete/uninstall older versions of Java?
    By LudwigKarl in forum New To Java
    Replies: 3
    Last Post: 03-19-2008, 10:23 AM
  2. I need help with my java servlet homework(average)
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-23-2007, 09:57 PM
  3. Help with my java servlet homework
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-21-2007, 05:41 PM
  4. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:41 AM
  5. Doubt in edit and delete methods
    By ai_2007 in forum Advanced Java
    Replies: 3
    Last Post: 07-01-2007, 11:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •