King Solomon and King David lived long and merry lives.
They had a lot of sweethearts; they had a lot of wives.
But when old age o'ertook them, with all its aches and qualms,
King Solomon wrote the Proverbs, and King David wrote the Psalms.
And Moses wrote the first five books, one of which records Moses's death.
THE DOCUMENTARY HYPOTHESIS
These videos explain it fairly well.