FileDocCategorySizeDatePackage
JDBCRentABike.javaAPI DocExample7439Sun Apr 24 19:38:20 BST 2005com.springbook

JDBCRentABike

public class JDBCRentABike extends org.springframework.jdbc.core.support.JdbcDaoSupport implements RentABike

Fields Summary
private String
storeName
private static final int
BIKE_ID
private static final int
BIKE_MANUFACTURER
private static final int
BIKE_MODEL
private static final int
BIKE_FRAME
private static final int
BIKE_SERIALNO
private static final int
BIKE_WEIGHT
private static final int
BIKE_STATUS
private static final int
CUST_ID
private static final int
CUST_FIRSTNAME
private static final int
CUST_LASTNAME
Constructors Summary
Methods Summary
public voiddeleteBike(Bike bike)

      JdbcTemplate template = getJdbcTemplate();
      template.execute("DELETE FROM bikes where bikes.id = " + bike.getBikeId() + "");
   
public BikegetBike(java.lang.String serialNo)

       final Bike bike = new Bike();
       JdbcTemplate template = new JdbcTemplate();
       template.setDataSource(this.getDataSource());

       class BikeHandler implements RowCallbackHandler {
           public void processRow(ResultSet rs) throws SQLException {
               bike.setBikeId(rs.getInt(BIKE_ID));
               bike.setManufacturer(rs.getString(BIKE_MANUFACTURER));
               bike.setModel(rs.getString(BIKE_MODEL));
               bike.setFrame(rs.getInt(BIKE_FRAME));
               bike.setSerialNo(rs.getString(BIKE_SERIALNO));
               bike.setWeight(rs.getDouble(BIKE_WEIGHT));
               bike.setStatus(rs.getString(BIKE_STATUS));
           }
       }

       template.query("SELECT * FROM bikes WHERE bikes.serialNo = '" + serialNo + "'",
                       new BikeHandler());
       return bike;
   
public BikegetBike(int bikeId)

          final Bike bike = new Bike();
          JdbcTemplate template = new JdbcTemplate();
          template.setDataSource(this.getDataSource());

          class BikeHandler implements RowCallbackHandler {
              public void processRow(ResultSet rs) throws SQLException {
                  bike.setBikeId(rs.getInt(BIKE_ID));
                  bike.setManufacturer(rs.getString(BIKE_MANUFACTURER));
                  bike.setModel(rs.getString(BIKE_MODEL));
                  bike.setFrame(rs.getInt(BIKE_FRAME));
                  bike.setSerialNo(rs.getString(BIKE_SERIALNO));
                  bike.setWeight(rs.getDouble(BIKE_WEIGHT));
                  bike.setStatus(rs.getString(BIKE_STATUS));
              }
          }

          template.query("SELECT * FROM bikes WHERE bikes.bikeId = " + bikeId,
                          new BikeHandler());
          return bike;
      
public java.util.ListgetBikes()



      
       final ArrayList results = new ArrayList();
       JdbcTemplate template = new JdbcTemplate();
       template.setDataSource(this.getDataSource());

       class BikesHandler implements RowCallbackHandler {
           public void processRow(ResultSet rs) throws SQLException {
               Bike bike = new Bike(rs.getString(BIKE_MANUFACTURER),
                     rs.getString(BIKE_MODEL), rs.getInt(BIKE_FRAME),
                     rs.getString(BIKE_SERIALNO), rs.getDouble(BIKE_WEIGHT),
                     rs.getString(BIKE_STATUS));
               results.add(bike);
           }
       }

       template.query("SELECT * FROM bikes", new BikesHandler());
       return results;
   
public CustomergetCustomer(int custId)

        final Customer customer = new Customer();
        JdbcTemplate template = new JdbcTemplate();

        class CustomerHandler implements RowCallbackHandler {
            public void processRow(ResultSet rs) throws SQLException {
                customer.setCustId(rs.getInt(CUST_ID));
                customer.setFirstName(rs.getString(CUST_FIRSTNAME));
                customer.setLastName(rs.getString(CUST_LASTNAME));
            }
        }

        template.query("SELECT * FROM customers WHERE customers.custId = " + custId,
                        new CustomerHandler());
        return customer;

    
public java.util.ListgetReservations(Customer customer)

      return new FindReservationsByCustomer(this.getDataSource()).
            findReservations(customer.getCustId());
   
public java.util.ListgetReservations(Bike bike)

      return new FindReservationsByBike(this.getDataSource()).
            findReservations(bike.getBikeId());   
   
public java.lang.StringgetStoreName()

      return this.storeName;
   
public voidsaveBike(Bike bike)

      JdbcTemplate template = getJdbcTemplate();
      Bike orig = this.getBike(bike.getSerialNo());
      System.out.println(orig);
      if(orig.getBikeId() == 0) {
          template.execute("INSERT INTO bikes (manufacturer, model, frame, serialno, weight, status) VALUES ('" + bike.getManufacturer() + "', '" + bike.getModel() + "', " + bike.getFrame() + ", '" + bike.getSerialNo() + "', " + bike.getWeight() + ", '" + bike.getStatus() + "')");
      }  else {
          template.execute("UPDATE bikes SET manufacturer = '" + bike.getManufacturer() + "', model = '" + bike.getModel() + "', frame = " + bike.getFrame() + ", serialno = '" + bike.getSerialNo() + "', weight = " + bike.getWeight() + ", status = '" + bike.getStatus() + "' WHERE bikeId = " + bike.getBikeId());
      }
   
public voidsetStoreName(java.lang.String name)

      this.storeName = name;