Question

I just need a second set of eyes on this as mine are not able to see the mistake.The insert is not working and I can't see why. This is for a homework assignment. The applicable code is inside the CustomerDAO class. This is for a webapp built with Java using the Stripes framework. Here is the offending code:

public void create(Customer customer) {
    String sql = "insert into customer (`firstname`, `lastname`, `email`, " +
            "`username`, `password`) values (?, ?, ?, ?, ?)";   
    try {
        PreparedStatement sth = this.dbh.getCon().prepareStatement(sql);
        sth.setString(1, customer.getFirstName());
        sth.setString(2, customer.getLastName());
        sth.setString(3, customer.getEmailAddress());
        sth.setString(4, customer.getUserName());
        sth.setString(5, customer.getPassword());
        sth.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();;
    } catch (NullPointerException ex) {
        ex.printStackTrace();
    } finally {
        this.dbh.closeConnection();
    }

}

As I said this method resides in my DAO. This method is called from an action bean for the registration form. Here are the things I have tested for:

The customer object is not null. The customer fields are getting their values from the form and they are correct. The DAO is not null. The database is connected.

I checked the items above by testing the condition in the Action Bean and returning null if the condition was true, otherwise I returned the RedirectResolution.

I did comment out the five lines that bind the variables to the SQL and replaced the variables in the SQL with the actual data I wanted to insert and the record did insert. That is why I believe the problem is somewhere in these five lines:

sth.setString(1, customer.getFirstName());
sth.setString(2, customer.getLastName());
sth.setString(3, customer.getEmailAddress());
sth.setString(4, customer.getUserName());
sth.setString(5, customer.getPassword());

When I used this if statement in the action bean I was redirected to a completely empty page which is how I know that the values are bound to the model's fields.

if (this.customer.getFirstName().equals("Tony")) {
    return null;
}

Here is the submit method from the action bean:

public Resolution submit() {
    this.customer = this.getCustomer();
    this.customerDao = this.getCustomerDao();
    this.customerDao.create(customer);
    return new  RedirectResolution(RegisterFormActionBean.class);
}

The test for the field values was inserted under this.customer = this.getCustomer();. I ran that test independently on all of the form fields and it always forwarded to a blank page. When this runs I am redirected back to the registration page but the record is not inserted. I checked the documentation to make sure that the setString() method is right and it appears to be.

I know it is probably something silly that I am overlooking but I am just spinning my wheels here.

Actual question: Why does the insert work when I hard code the values into the SQL but it doesn't work when I bind values to the statement.

Was it helpful?

Solution 2

I figured it out. On a hunch I changed my database table to accept NULL except for the id field. When I ran it again the record inserted and I saw that the 'password' field was NULL. I have two password inputs on the register.jsp page, both named 'password'. Apparently Stripes doesn't know what to do if you have duplicate names on the same form, I thought one would overwrite the other. I renamed one of the text boxes to "" and changed the database table back to not allowing NULL and the record inserted.

OTHER TIPS

Perhaps your transactions are not being autocommitted. Verify how your dbh object is creating connections and see if they are disabling autocommit. Try enabling autocommit with this:

Connection myConnection = this.dbh.getCon();
myConnection.setAutoCommit(true);
myConnection.prepareStatement(sql);

See http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top