コード例 #1
0
  public void delete(VehicleType vehicleType) throws RARException {
    String deleteVTSql = "delete from VehicleType where vehicleTypeId = ?";
    PreparedStatement stmt = null;
    int inscnt;

    if (!vehicleType
        .isPersistent()) // is the VehicleType object persistent?  If not, nothing to actually
                         // delete
    return;

    try {
      stmt = (PreparedStatement) conn.prepareStatement(deleteVTSql);
      stmt.setLong(1, vehicleType.getId());
      inscnt = stmt.executeUpdate();
      if (inscnt == 1) {
        return;
      } else throw new RARException("VehicleTypeManager.delete: failed to delete a VehcleType");
    } catch (SQLException e) {
      e.printStackTrace();
      throw new RARException("VehicleTypeManager.delete: failed to delete a VehicleType: " + e);
    }
  }
コード例 #2
0
  public void save(VehicleType vehicleType) throws RARException {
    String insertVTSql = "insert into VehicleType ( typeName  ) values ( ? )";
    String updateVTSql = "update VehicleTye set typeName = ? where id = ?";
    PreparedStatement stmt = null;
    int inscnt;
    long VTId;

    /*
    if( club.getFounderId() == -1 )
        throw new ClubsException( "ClubManager.save: Attempting to save a Club without a founder" );
        */
    try {

      if (!vehicleType.isPersistent())
        stmt = (PreparedStatement) conn.prepareStatement(insertVTSql);
      else stmt = (PreparedStatement) conn.prepareStatement(updateVTSql);

      if (vehicleType.getType() != null) // type is unique and non null
        // This is me setting the value in col typeName in table VehicleType to the provided value
        stmt.setString(1, vehicleType.getType());
      else
        throw new RARException("VehicleTypeManager.save: can't save a VehicleType: name undefined");
      /*
       * This is an example for when there are more parameters to deal with
                  if( club.getAddress() != null )
                      stmt.setString( 2, club.getAddress() );
                  else
                      stmt.setNull( 2, java.sql.Types.VARCHAR );

                  if( club.getEstablishedOn() != null ) {
                      java.util.Date jDate = club.getEstablishedOn();
                      java.sql.Date sDate = new java.sql.Date( jDate.getTime() );
                      stmt.setDate( 3,  sDate );
                  }
                  else
                      stmt.setNull(3, java.sql.Types.DATE);

                  if( club.getFounder() != null && club.getFounder().isPersistent() )
                      stmt.setLong( 4, club.getFounder().getId() );
                  else
                      throw new ClubsException( "ClubManager.save: can't save a Club: founder is not set or not persistent" );
      */
      if (vehicleType.isPersistent()) stmt.setLong(2, vehicleType.getId());

      inscnt = stmt.executeUpdate();

      if (!vehicleType.isPersistent()) {
        if (inscnt >= 1) {
          String sql = "select last_insert_id()";
          if (stmt.execute(sql)) { // statement returned a result

            // retrieve the result
            ResultSet r = stmt.getResultSet();

            // we will use only the first row!
            //
            while (r.next()) {

              // retrieve the last insert auto_increment value
              VTId = r.getLong(1);
              if (VTId > 0) vehicleType.setId(VTId); // set this vehicleType's db id (proxy object)
            }
          }
        } else throw new RARException("VehicleTypeManager.save: failed to save a VehicleType");
      } else {
        if (inscnt < 1)
          throw new RARException("VehicleTypeManager.save: failed to save a VehicleType");
      }
    } catch (SQLException e) {
      e.printStackTrace();
      throw new RARException("VehicleTypeManager.save: failed to save a VehicleType: " + e);
    }
  }