The Gemara in Shabbos 56a relates the following:
R. Shmuel bar Nahmeni said in the name of R. Yonasan: Whoever says that David sinned is nothing but mistaken, as it says "And David behaved wisely in all his ways, and the Lord was with him..." (1 Shmuel 18:14). Is it possible that a sin came to his hands and the Divine Presence was with him? If so, how do I explain "Why have you despised the commandment of the Lord, to do evil..." (2 Shmuel 12:9)? He wished to do [sin] but did not.What emerges clearly from this passage is that one who claims that David sinned regarding Bassheva and Uriah (the context of the latter verse) is mistaken. Not a heretic, mind you, but mistaken. However, claiming that one's personal view is correct and the Talmud not does have distinct shades of heresy. Who would dare to contradict an explicit passage of the Talmud?
With this passage in hand, it is possible to dismiss the entire story of David's sin as an intentional exaggeration by the biblical author. David wanted to sin but did not, and for someone on his level that is sufficient to deserve the punishments he received. (The apparent contradiction with Kiddushin 39b regarding mahashavah ra'ah could possibly be explained by distinguishing between thinking about sinning, which is not punished, and attempting to sin, which is. Or, perhaps, the passage in Kiddushin only applies to average people while exceptional people like David are judged on a higher standard.)
However, the sensitive reader of Tanakh cannot help but notice the distinct midah ke-neged midah in the punishments of David that correspond very clearly to the sins of adultery and murder - his child from Bassheva dies, his daughter is raped, his son becomes a rapist and another son rebels against and tries to kill him. The correspondence is even pointed out by the classical commentators in the Mikra'os Gedolos. Particularly difficult is the death of the young child. If he was the offspring of an illicit encounter between David and Bassheva then it is understandable why an appropriate punishment is the death of the fruits of that sin (aside from the obvious question about punishing an innocent child). But if the child was not from an adulterous relationship but from the time period after David legally married Bassheva, the correspondence to the (non-)sin is much weaker if existent at all.
There are other passages in the Talmud that directly address this story in a much different manner. The Gemara in Avodah Zarah 4b-5a:
R. Yohanan said in the name of R. Shimon bar Yohai: David was not the kind of man to do that act... as it is says "My heart is slain within me" (Tehillim 119:22)... Why, then, did [he] act thus? In order to teach that if an individual sins [and hesitates about the effect of repentance] he can be referred to the individual [David]... This accords with the following saying of R. Shmuel bar Nahmeni in the name of R. Yonasan: What is the meaning of the verse "The saying of David the son of Yishai, and the saying of the man raised on high" (2 Shmuel 23:1)? The saying of David the son of Yishai, the man who elevated the yoke of repentance.There are a few things to note here. First, it is clear that David sinned, although his intention is somewhat justified in that he did so in order to teach about teshuvah. However, he sinned. Second, a saying from R. Shmuel bar Nahmeni in the name of R. Yonasan is quoted, this time implying that David sinned and repented. Yet, in Shabbos above the same pair is quoted as saying that David definitely did not sin.
And then there is the following in Yoma 22b:
Rav said: ...David [sinned] with two and they did not count against him... What are they? Uriah and counting [the nation's population]. But what about the incident with Bassheva? There, he was punished for it, as it says "And he shall restore fourfold for the lamb" (2 Shmuel 12:6) - [his four punishments were:] the child, Amnon, Tamar and Avshalom...The clear statement here is that David sinned with Bassheva and was punished for it. It seems that there is a more nuanced view emerging from the Talmud than one would have thought by just looking at the passage in Shabbos. There is more than one way to reconcile these seemingly conflicting passages and we will present two.
1. There is a disagreement among the sages whether David sinned or not. According to the passage in Shabbos, David tried to sin but did not. According to the passages in Avodah Zarah and Yoma, he did sin. There is no contradiction between the views of R. Shmuel bar Nahmeni in the name of R. Yonasan because in Avodah Zarah they are only quoted as saying that David taught the importance of teshuvah. This was utilized as part of an approach that understood the biblical text as saying that David sinned, but we are not forced to conclude that R. Shmuel bar Nahmeni and R. Yonasan would have agreed to that. He could have taught about teshuvah in other ways.
2. Everyone agrees that David did not sin. He only tried to sin but did not. However, for someone on his high level this was so egregious a transgression that he was punished extremely harshly for it, as if he had actually sinned. Thus, his punishments correspond to what he had tried to do rather than what he actually did. His repentance was appropriate because he had put in a good deal of thought and effort into sinning, even though his plan did not succeed, and that time and energy spent on sin was terribly damaging to his soul. He needed repentance, and taught us much through his own process of change.
I find the latter explanation to be more compelling from the perspective of talmudic interpretation. However, I do not deny others the option of preferring the former explanation and explaining that David did, in fact, sin, so long as they leave open the possibility of the other talmudic view and do not denigrate it.
In this respect, R. Carmy has a great take on the David and Bassheva episode from the previously mentioned article in Hamevaser:
The Gemara suggests that David didn't commit adultery because Uriah had given Batsheva a conditional get, and that Uriah's inferred disloyalty made his life forfeit and therefore exculpated David from the guilt of his death. Abarbanel questions this, and the text of Tanakh seems to support him. After all, David was punished for taking Batsheva and for killing Uriah. According to Abarbanel, then, and according to the simple phrasing of Tanakh, David was an adulterer; according to Hazal he was not.That Tanakh presents David as sinning, even though according to the sages he did not, teaches important lessons about morality and misuses of halakhic legalities.
Which view is historically correct? If the Gemara is conveying the authentic tradition of Torah she-b’al Peh, then it is literally true, and you have to explain why the pasuk gives a different impression. If Abarbanel is right, then the Gemara, regarding David as a righteous person, is offering the most respectful, least damaging version of the story.
It is not my primary interest to decide between these options. My business is to explore the implications of the sources. Why indeed does the Navi imply that David was an adulterer and a murderer, why is he so severely punished for his behavior, if, as Hazal teach, he was halakhically impregnable? The answer is very simple. Legal invulnerability does not exclude moral guilt. In the face of God's condemnation, David's ability to justify himself on narrow halakhic grounds counts for very little. We, who have so much experience with legalistic politicians and other amoral personages, should understand why Hazal's defense of David does not override peshuto shel mikra.