The officer who has charge of the boats, sails, rigging, anchors, cordage, cables, and colours. Swain is the Saxon swein (a boy, servant), Swedish sven. Hence, a shepherd is a swain, and a sweetheart is a woman's servant or swain.
Source: Dictionary of Phrase and Fable, E. Cobham Brewer, 1894
More on Boatswain from Infoplease: