Despite his flaws David is still seen as the great king of Israel. Perhaps part of the reason we see in today's first reading. His son is out to kill him, it all seems to be coming apart, and a poor an comes out cursing and throwing rocks at him.
One of David's men wants to lop off his head for insulting the king. David's response: Suppose the LORD has told him to curse David; who then will dare to say, 'Why are you doing this?'"
David knows the sins he has committed. David doesn't play the innocent victim. He is able to look at this a see that perhaps it is all part of God's plan. He is able to see when the situation he is in is of his own making.
There is true wisdom.