Updating a microsoft Access Database using JSP

Associate
Joined
31 May 2005
Posts
328
Location
Surrey
Hi, im currently coding a simple java app that connects to a database which adds, edits and deletes contacts. I have managed to get the adding and deleting working however I cant get it to update. I have a sql statement in the DBConnector java class and two jsp pages (UpdateContactForm.jsp) and (UpdateContact.jsp)

"UpdateContactForm.jsp"

Code:
<%@page import="java.sql.*" %>
<%@page import="hooyaclasses.DBConnector" %>

<jsp:useBean id="db" scope="page" class="hooyaclasses.DBConnector" />
<jsp:useBean id="contact" scope="page" class="hooyaclasses.ContactDetails" />

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hooya Contact Manager - Admin System</title>
    </head>
    <body>
        <h3>Update Contact Details</h3>
        <hr>

        <%
        String displayName;

       displayName = request.getParameter("displayname");

        db.createConnection();
        contact = db.selectContactByDisplayName(displayName);
        db.closeConnection();
        %>

        Please make the amendments you require: <br><br>
        <form name="amend" method="post" action="UpdateContact.jsp" >
            DisplayName: <%= contact.getDisplayName()%><br><br>
            Address:
            <input type="text" name="address" value="<%= contact.getAddress()%>" /><br>
            <br><br>
            <input type="submit" name="button" value="Update" />
            <input type="submit" name="button" value="Cancel" />
        </form>
    </body>
</html>

this grabs the current list of names from the database and then allows me to edit the email address of the contact. when i press "Update" the page goes to

"UpdateContact.jsp"

Code:
<%@page import="java.sql.*" %>
<%@page import="hooyaclasses.DBConnector" %>

<jsp:useBean id="db" scope="page" class="hooyaclasses.DBConnector" />
<jsp:useBean id="contact" scope="session" class="hooyaclasses.ContactDetails" />

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hooya Contact Management system - Admin System</title>
    </head>
    <body>

                            <h3>Update Confirmation</h3>
        <hr>
            <jsp:setProperty name="contact" property="*" />

        <%
        if (request.getParameter("Amend").equals("Cancel"))
            response.sendRedirect("ContactAdmin.jsp");
      
        
         
                db.createConnection();
                db.updateContact(contact);
                db.closeConnection();
            %>

            <br>
                 <form name="amend" method="post" action="UpdateContact.jsp" >
            Address <%= contact.getDisplayName() %> has been updated.
            <br><br>
            <a href="ContactAdmin.jsp">Back to the admin home page</a>
    </form>
    </body>
  
</html>

after confirming the update it says that the contact was updated however when I then go and look at the list of contacts the address has not changed

Here is the UpdateContact class in the DBConnector

Code:
[SIZE="3"]    public void updateContact(ContactDetails contact) {
        try {
            String strQuery = "UPDATE " + contact +
                    " SET address= ? " +
                    " WHERE displayname= ? ";

            PreparedStatement stmt = conn.prepareStatement(strQuery);
            stmt.setString(1, contact.getAddress());
            stmt.setString(2, contact.getDisplayName());
            stmt.executeUpdate(strQuery);
        }
         catch (SQLException e) {
            e.printStackTrace();
        }
    }[/SIZE]

Any help or tips would be much appreciated!

Cheers Nick
 
Back
Top Bottom