Updating Data

Fast and efficient selects are important; however, applications must also be able to update existing data. Updates are surprisingly less complicated than selects. Because update operations do not return any data, you do not need to create abstract selection classes that map the results to objects. To perform an update, we need to create a simple subclass of SqlUpdate, specify the SQL statement that performs the update, and declare parameters the SQL statement expects (see Listing 8-18).

Listing 8-18: Implementation of an Update

image from book
public class JdbcTestDao           extends JdbcDaoSupport implements TestDao {          private static final String UPDATE_SQL =          "update Test set Name=? where TestId=?";          abstract class AbstractSelect extends MappingSqlQuery {         /* omitted for clarity */      }          class SelectByName extends AbstractSelect { /* omitted for clarity */ }          class Update extends SqlUpdate {         public Update(DataSource dataSource) {             super(dataSource, UPDATE_SQL);             declareParameter(new SqlParameter(Types.VARCHAR));    // Name             declareParameter(new SqlParameter(Types.INTEGER));    // TestId         }     }          private SelectByName selectByName;     private Update update;          protected void initDao() throws Exception {         super.initDao();         selectByName = new SelectByName(getDataSource());         update = new Update(getDataSource());     }          public void update(Test test) {         update.update(new Object[] { test.getName(),              new Integer(test.getTestId()) });     }      }
image from book

The code is very simple; the only thing you need to be mindful of is the order of the values in the values array.

A special case of an update is a delete. To our code, a delete operation is the same as an update operation. It takes a number of parameters and does not return any data. Hence the Delete class is also a subclass of SqlUpdate.

Listing 8-19: Delete Class Declaration and Implementation

image from book
public class JdbcTestDao           extends JdbcDaoSupport implements TestDao {          private static final String UPDATE_SQL =          "update Test set Name=? where TestId=?";     private static final String DELETE_SQL =          "delete from Test where TestId=?";          abstract class AbstractSelect extends MappingSqlQuery {         /* omitted for clarity */      }          class SelectByName extends AbstractSelect { /* omitted for clarity */ }          class Update extends SqlUpdate { /* omitted for clarity */ }          class Delete extends SqlUpdate {         public Delete(DataSource dataSource) {             super(dataSource, DELETE_SQL);             declareParameter(new SqlParameter(Types.INTEGER));    // TestId         }     }          private SelectByName selectByName;     private Update update;     private Delete delete;          protected void initDao() throws Exception {         super.initDao();         selectByName = new SelectByName(getDataSource());         update = new Update(getDataSource());         delete = new Delete(getDataSource());     }          public void delete(int testId) {         delete.update(testId);     }      }
image from book

The Delete class calls the constructor of the superclass with the dataSource parameter and a specific SQL delete query. Just as we did for the update and select classes, we create an individual delete class for each delete operation.

Again, this code is very simple and because we are passing only a single int parameter, we can take advantage of one of the overloaded execute() methods that takes one int parameter. We do not need to create an array of Objects containing a single Integer.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net