The King at that time was Saul. He wasn't too good of a king.
War broke out between the Philistines and the Israelites. Three of David's brothers were soldiers so he had to help his father out on his farm.
David played the harp wonderfully, he was even asked to play his harp at the palace for the king.
David was so good, king Saul offered him to stay at the palace.
King Saul's oldest son Jonathan was a good friend to David. They were like brothers. He loved staying at the palace. But unfortunately the young shepard's boy had to go back to his fathers house because:
Since three of his brothers were soldiers, David will sometimes bring bread and water for them and also watch them fight. David was outraged when he saw Goliath make a mockery of God.
God gives me the strength and courage to fight that ignorant Philistine.
He went to Saul and told him that he'll fight Goliath. That he had slain so many lions and bears who tried to steal his fathers sheep.