In 1 Samuel 16, God anoints David, the person who will be the ancestor of the Messiah, as king. David comes from the tribe of Judah. “The scepter shall not depart from Judah, nor the ruler's staff from between his feet, until Shiloh come, and the nations shall obey Him.” Genesis 49 verse 10.
King Saul knows he has been rejected by God; 1 Samuel 15 verse 26. He suspects that David will become king in his place. 1 Samuel 18 verse 8. He wants to kill David out of revenge and jealousy.
In 1 Samuel 24, David has fled from Saul. After previously hiding in other places, he now hides, together with his 600 men, in the oasis Ein Gedi, in the Judean desert, near the Dead Sea.
Saul chases after him with 3,000 of his best men. David and his men are hidden in a large cavern. It is precisely in that cave that Saul comes to relieve himself. David has the opportunity to kill Saul; God, as it were, gives him into his hand. But David shows respect for Saul as God's anointed king. Although David has already been anointed king over Israel, he waits for God's time and does not incur bloodguilt.
David shows himself here a pre-image of Jesus, the Messiah. He remains standing in this trial, as did Jesus later. Matthew 4 verse 1 to 11. He shows love to his enemy, and blesses the one who curses him. Matthew 5 verse 44. Jesus prays for those who crucify him. Luke 23 verse 34.
Like David, Jesus also suffered before He was exalted as King.
Then David calls Saul to account. He proves that he means no harm to Saul by showing him the corner of Saul's cloak, which he has cut off.
He appeals to the LORD who is the all-knowing Judge between Saul and him.
This history shows that God protected David's life. He did this for the purpose of establishing David's royal house as the dynasty that would produce the Eternal King and Messiah.
To this end the LORD made His covenant with David:
2 Samuel 7 verse 16
‘And your house and your kingdom shall be made sure forever before me.[c] Your throne shall be established forever.’
See also:
Isaiah 9 verse 6,7:
‘For to us a child is born,
to us a son is given;
and the government shall be upon his shoulder,
and his name shall be called
Wonderful Counselor, Mighty God,
Everlasting Father, Prince of Peace.
Of the increase of his government and of peace
there will be no end,
on the throne of David and over his kingdom,
to establish it and to uphold it
with justice and with righteousness
from this time forth and forevermore'.
Jeremiah 23 verses 5,6
‘Behold, the days are coming, declares the Lord, when I will raise up for David a righteous Branch, and he shall reign as king and deal wisely, and shall execute justice and righteousness in the land. In his days Judah will be saved, and Israel will dwell securely. And this is the name by which he will be called: ‘The Lord is our righteousness.’
Jeremiah 33 verse 17:
‘For thus says the Lord: David shall never lack a man to sit on the throne of the house of Israel,..’
Ezekiel 34 verses 23, 24
‘And I will set up over them one shepherd, my servant David, and he shall feed them: he shall feed them and be their shepherd. And I, the Lord, will be their God, and my servant David shall be prince among them. I am the Lord; I have spoken’.
Psalm 89 verses 3, 4
You have said, “I have made a covenant with my chosen one;
I have sworn to David my servant:
‘I will establish your offspring forever,
and build your throne for all generations.’” Selah
Matthew 9 verse 27
‘And as Jesus passed on from there, two blind men followed him, crying aloud, “Have mercy on us, Son of David.’
Luke 1 verses 32, 33
‘He will be great and will be called the Son of the Most High. And the Lord God will give to him the throne of his father David, and he will reign over the house of Jacob forever, and of his kingdom there will be no end.’