David, like Dimmesdale, was a pious man who stumbled and committed adultery. Also, like Dimmesdale, he was greatly troubled about his sin. See Psalm 51:
For the director of music. A psalm of David. When the prophet Nathan came to him after David had committed adultery with Bathsheba.
1 Have mercy on me, O God,
according to your unfailing love;
according to your great compassion
blot out my transgressions.
2 Wash away all my iniquity
and cleanse me from my sin.
3 For I know my transgressions,
and my sin is always before me.
4 Against you, you only, have I sinned
and done what is evil in your sight,
so that you are proved right when you speak
and justified when you judge.
5 Surely I was sinful at birth,
sinful from the time my mother conceived me.
7 Cleanse me with hyssop, and I will be clean;
wash me, and I will be whiter than snow.
8 Let me hear joy and gladness;
let the bones you have crushed rejoice.
9 Hide your face from my sins
and blot out all my iniquity.
10 Create in me a pure heart, O God,
and renew a steadfast spirit within me.
11 Do not cast me from your presence
or take your Holy Spirit from me.
12 Restore to me the joy of your salvation
and grant me a willing spirit, to sustain me.
13 Then I will teach transgressors your ways,
and sinners will turn back to you.
14 Save me from bloodguilt, O God,
the God who saves me,
and my tongue will sing of your righteousness.
15 O Lord, open my lips,
and my mouth will declare your praise.
In addition, David suffered a number of misfortunes following his sin with Bathsheba:
a) an infant son died (2 Samuel 12:15-18)
b) his daughter, Tamar, was raped (2 Samuel 13)
c) his son Absalom rebelled against him and was eventually killed (2 Samuel 15)
In The Scarlet Letter, Dimmesdale does not suffer these kinds of misfortunes, but as a consequense of his sin, his physical health suffers greatly.
In sum, it is no coincidence that Hawthorne put a picture of David and Bathsheba in Dimmesdale's room.
The tapestries on the walls in Dimmesdale's room show the complete story of David and Bathsheba, who committed adultery with each other just as Dimmesdale committed adultery with Hester. The story of David and Bathsheba is in the Old Testament of the Bible.