The Panama Canal was built for both commercial and military reasons.
Commercially, the Panama Canal was extremely important because it reduced the travel time from the East Coast of the US to the West Coast (or vice versa, of course) dramatically. People knew that a canal across Panama would be extremely beneficial to those wanting to trade or travel from coast to coast. This was one reason for the canal.
Militarily, the Spanish-American War helped lead to the building of the canal. During that war, it took the USS Oregon 10 weeks to get from San Francisco to Cuba to participate in a blockade of the island. This illustrated how vital the canal could be since it would allow warships to move from ocean to ocean much more quickly.
Thus, the Panama Canal was important for both commercial and military reasons.