Nevertheless, for David's sake YHWH his God gave him a lamp in Jerusalem, setting up his son after him, and establishing Jerusalem, because David did what was right in the eyes of YHWH and did not turn aside from anything that he commanded him all the days of his life, except in the matter of Uriah the Hittite.
1 Kings 15:4-5

How does Scripture say that David did what was right in God's eyes when he did so much that was wrong, not just with Bathsheba and Uriah? Answer: he was never in rebellion against God. He always wanted to do the right thing, even when he was weak and gave into temptations.