Now to the one who works, his wages are not  counted as a gift but as his due. And to the one who does not work but  believes in him who justifies the ungodly, his faith is counted as  righteousness, just as David also speaks of the blessing of the one to  whom God counts righteousness apart from works:
“Blessed are those whose lawless deeds are forgiven,
and whose sins are covered;
blessed is the man against whom the Lord will not count his sin.” [from Romans 4]
This is a transitional moment for David. Later, Paul places him squarely with Christ against the Israelite unbelievers. From Romans 11:
And David says,
“Let their table become a snare and a trap,
a stumbling block and a retribution for them;
let their eyes be darkened so that they cannot see,
and bend their backs forever.”
Since Jesus is introduced in Romans 1.3 as “born of the seed of David according to the flesh,” one might guess that David represents the best in Israel all along. But no, after the introduction and before the blessedness of not having sins counted against him in Romans 4, David pretty much stands for unbelieving Israel. For in Romans 2 we read,
You who boast in the law dishonor God by  breaking the law. For, as it is written, “The name of God is blasphemed  among the Gentiles because of you.”
And this reminds us of what happened to David after he sinned with Bathsheba.
As Nathan told David: “because by this deed you have given occasion to the enemies of the LORD to blaspheme, the child also that is born to you shall surely die” (2 Sam. 12.14; NASB).
And so David prays what we now know as Psalm 51, which Paul uses as his next OT quotation to show the unfaithfulness of Israel:
Then what advantage has the Jew? Or what is the  value of circumcision? Much in every way. To begin with, the Jews were  entrusted with the oracles of God. What if some were unfaithful? Does  their faithlessness nullify the faithfulness of God? By no means! Let  God be true though every one were a liar, as it is written,
“That you may be justified in your words,
and prevail when you are judged.”
But if our  unrighteousness serves to show the righteousness of God, what shall we  say? That God is unrighteous to inflict wrath on us? (I speak in a human  way.) By no means! For then how could God judge the world? But if  through my lie God’s truth abounds to his glory, why am I still being  condemned as a sinner? And why not do evil that good may come?—as some  people slanderously charge us with saying. Their condemnation is just.
The story of David and Bathsheba fits perfectly. The reign of Solomon is described in a way that shows that God has completely fulfilled the promises made to Abraham. And how did Solomon come about? David sinned and lost a son before being given Solomon, or, more literally, “Peace.”
Then David comforted his wife, Bathsheba, and went in to her and lay  with her, and she bore a son, and he called his name Solomon Peace. And the LORD loved him and sent a message by Nathan the prophet. So he called his name Jedidiah, because of the LORD.
Therefore, since we have been justified by faith,  we have peace with God through our Lord Jesus Christ. Through him we  have also obtained access by faith into this grace in which we stand,  and we rejoice in hope of the glory of God. More than that, we rejoice  in our sufferings, knowing that suffering produces endurance, and  endurance produces character, and character produces hope, and hope does  not put us to shame, because God’s love has been poured into our hearts  through the Holy Spirit who has been given to us. For while we were still weak, at the right time  Christ died for the ungodly. For one will scarcely die for a righteous  person—though perhaps for a good person one would dare even to die— but  God shows his love for us in that while we were still sinners, Christ  died for us. Since, therefore, we have now been justified by his blood,  much more shall we be saved by him from the wrath of God. For if while  we were enemies we were reconciled to God by the death of his Son, much  more, now that we are reconciled, shall we be saved by his life. More  than that, we also rejoice in God through our Lord Jesus Christ, through  whom we have now received reconciliation.
For David “would not have known what it is to covet if the law had not said, ‘You shall not covet.’ But sin, seizing an opportunity through the commandment, produced in” David a coveting for Bathsheba (c.f. Romans 7). Yet God fulfilled all his covenant promises to the Patriarchs not through David’s faithfulness but through this very unfaithfulness, though it required the death of his firstborn son. And so God’s son, the greater David does the same as his infant type:
For I tell you that Christ became a servant to the circumcised to show God’s truthfulness, in order to confirm the promises given to the patriarchs, and in order that the Gentiles might glorify God for his mercy. As it is written,
“Therefore I will praise you among the Gentiles,
and sing to your name.”
This too, of course, is from David (2 Samuel 22.50; Psalm 18.49).