Exercise is important for many reasons, but not for preventing obesity. Unless you're running marathons, or exercising like somebody who is, the amount of calories you will burn from exercising will in no way whatsoever compare to what you eat. If you start eating more because you go to the gym, you'll almost certainly end up gaining weight.
The answer today is the same as it's been for thousands of years.