Servomotor losing its connection right after being connected to Arduino
Your code is correct. Check the following:


Arduino works from 5V, but most servos need 6V. 
Even if your servo works from 5V, don't ever connect your servo power directly to Arduino pins. These pins are not able to provide adequate power for servo.
Check that grounds of both power supply for Arduino and power supply for servo are connected together.