James Buchanan, military service