I've read all the stories of David in Scripture and if I were transported back to his day, I wouldn't trust him behind my back! Particularly think of his interaction with Uriah and Bathsheba (2 Sam.11:1-27)---coveting, adultery with another man's wife, then murder of her husband! David broke at least three of the ten commandments in that short time frame. (Exod. 20) The prescribed punishment was “both the adulterer and the adulteress are to be put to death.” (Lev. 20:10) For murder, “Anyone who kills a person is to be put to death as a murderer only on the testimony of witnesses.” (Num. 35:30) There were other problems too: He took the protection and hospitality of Achish, king of Gath, but raided the towns around and killed every man and woman so Achish wouldn’t find out. (1 Sam.27:6-12) Later on, he sent his own daughter Tamar to disgrace, nor did he punish his sons, Amnon and Absalom. (2 Sam.13:7; 2 Sam.14:23-24) One final example: David was told not to count the fighting men but he did anyway and seventy thousand died of plague as a result. (2 Sam.24:1-2)
David’s judgment against himself in the matter of Bathsheba (albeit, given unknowingly) was, “As surely as the Lord lives, the man who did this must die! “ (2 Sam. 12:5) By all rights David and Bathsheba should both have been executed, but Nathan told him, “The Lord has taken away your sin. You are not going to die… But… you have shown utter contempt for the Lord…” (2 Sam.12:13-14)
Now this is where Psalm 51 comes in; this is where we may start to see an answer to the question in our title. The heading from ancient times reads, “A psalm of David. When the prophet Nathan came to him after David had committed adultery with Bathsheba.” David begs for mercy from God; he admits his transgressions, his iniquity, his sin, his evil, his guilt. What other words could he use? Then he begs for mercy from God, blotting out of his transgressions, washing, cleansing, joy, gladness, rejoicing, a pure heart, a steadfast spirit, deliverance, and especially the presence of God’s Holy Spirit.
David was completely shattered. There was nothing left of his pride, his arrogance, or his “entitlement”. Remember what he did at this time? The baby conceived with Bathsheba was born sickly so David “pleaded with God for the child. He fasted and spent the nights lying in sackcloth on the ground… On the seventh day the child died.” This is where David's attitude turned around. Now he cleaned himself up and “went into the house of the Lord and worshiped.” If this isn't when he wrote Psalm 51, it is at least an expression of his experiences of that time.
Psalm 51 continues with heartfelt promises---David will “teach transgressors your ways”, his “tongue will sing of your righteousness”, his “mouth will declare your praise”, above all, he sacrifices “a broken and contrite heart.” Samuel had told King Saul that “the Lord has sought out a man after his own heart…because you have not kept the Lord’s command.” (1 Sam.13:14) The apostle Paul paraphrased that to read, “I have found David son of Jesse, a man after my own heart.” (Acts 13:22) To help understand what that means, centuries later, Jeremiah got a promise from the Lord about Israel: “I will give you shepherds after my own heart, who will lead you with knowledge and understanding.” (Jer. 3:15) to
We know David was a prophet (Acts 2:29-30). It seems that in his senior years, David did get more “knowledge and understanding.” Look at Psalm 110. Jesus Himself quoted this passage and gave David credit for “speaking by the Holy Spirit” when he spoke about his Lord being his Son. (Mark 12:36) Then look at Psalm 22: this is another Psalm of David. This perhaps more than any other Scripture, brings us closest to the heart of God. This is the thoughts and words of God suffering for man’s sin. This is Jesus, Himself, on the cross: “My God, my God, why have you forsaken me?...All who see me mock me...all my bones are out of joint...they pierce my hands and my feet...He has done it!” (Psalm 22:1-31)
In the final analysis, we know that David was “a man after God’s own heart," not because Samuel or Paul tell us so but simply because he’s the man who wrote Psalm 22!