A famous rabbinic discussion of David's lapse, including the notion that his public reputation and the theological implications of the Bathsheba episode require nuance.
אָמַר רַב יְהוּדָה אָמַר רַב: לְעוֹלָם אַל יָבִיא אָדָם עַצְמוֹ לִידֵי נִסָּיוֹן, שֶׁהֲרֵי דָּוִד מֶלֶךְ יִשְׂרָאֵל הֵבִיא עַצְמוֹ לִידֵי נִסָּיוֹן וְנִכְשָׁל. אָמַר לְפָנָיו: רִבּוֹנוֹ שֶׁל עוֹלָם, מִפְּנֵי מָה אוֹמְרִים ״אֱלֹהֵי אַבְרָהָם, אֱלֹהֵי יִצְחָק וֵאלֹהֵי יַעֲקֹב״, וְאֵין אוֹמְרִים ״אֱלֹהֵי דָוִד״? אָמַר: אִינְהוּ מִינְּסוּ לִי וְאַתְּ לָא מִינַּסֵּית לִי. אָמַר לְפָנָיו: רִבּוֹנוֹ שֶׁל עוֹלָם, בְּחָנֵנִי וְנַסֵּנִי, שֶׁנֶּאֱמַר: ״בְּחָנֵנִי ה׳ וְנַסֵּנִי וְגוֹ׳״. אֲמַר: מְינַסֵּינָא לָךְ, וְעָבֵידְנָא מִילְּתָא בַּהֲדָךְ, דִּלְדִידְהוּ לָא הוֹדַעְתִּינְהוּ, וְאִילּוּ אֲנָא קָא מוֹדַעְנָא לָךְ דִּמְנַסֵּינָא לָךְ בִּדְבַר עֶרְוָה. מִיָּד, ״וַיְהִי לְעֵת הָעֶרֶב וַיָּקׇם דָּוִד מֵעַל מִשְׁכָּבוֹ וְגוֹ׳״. ״וַיִּתְהַלֵּךְ עַל גַּג בֵּית הַמֶּלֶךְ וַיַּרְא אִשָּׁה רוֹחֶצֶת מֵעַל הַגָּג וְהָאִשָּׁה טוֹבַת מַרְאֶה מְאֹד״. בַּת שֶׁבַע הֲוָה קָא חָיְיפָא רֵישָׁא תּוּתֵי חַלְּתָא. אֲתָא שָׂטָן, אִידְּמִי לֵיהּ כְּצִיפַּרְתָּא. פְּתַק בֵּיהּ גִּירָא, פַּתְקַהּ לְחַלְּתָא, אִיגַּלַּיה וְחַזְיַיהּ. מִיָּד: ״וַיִּשְׁלַח דָּוִד וַיִּדְרֹשׁ לָאִשָּׁה וַיֹּאמֶר הֲלוֹא זֹאת בַּת שֶׁבַע בַּת אֱלִיעָם אֵשֶׁת אוּרִיָּה הַחִתִּי. וַיִּשְׁלַח דָּוִד מַלְאָכִים וַיִּקָּחֶהָ וַתָּבוֹא אֵלָיו וַיִּשְׁכַּב עִמָּהּ וְהִיא מִתְקַדֶּשֶׁת מִטֻּמְאָתָהּ וַתָּשׇׁב אֶל בֵּיתָהּ״. וְהַיְינוּ דִּכְתִיב: ״בָּחַנְתָּ לִבִּי פָּקַדְתָּ לַּיְלָה צְרַפְתַּנִי בַל תִּמְצָא זַמֹּתִי בַּל יַעֲבׇר פִּי״. אָמַר: אִיכּוֹ זְמָמָא נְפַל בְּפוּמֵּיהּ דְּמַאן דְּסָנֵי לִי, וְלָא אֲמַר כִּי הָא מִילְּתָא! דָּרֵשׁ רָבָא: מַאי דִּכְתִיב ״כִּי אֲנִי לְצֶלַע נָכוֹן וּמַכְאוֹבִי נֶגְדִּי תָמִיד״? רְאוּיָה הָיְתָה בַּת שֶׁבַע בַּת אֱלִיעָם לְדָוִד מִשֵּׁשֶׁת יְמֵי בְּרֵאשִׁית, אֶלָּא שֶׁבָּאת עָלָיו בְּמַכְאוֹב. וְכֵן תָּנָא דְּבֵי רַבִּי יִשְׁמָעֵאל: רְאוּיָה הָיְתָה לְדָוִד בַּת שֶׁבַע בַּת אֱלִיעָם, אֶלָּא שֶׁאֲכָלָהּ פַּגָּה.
§ Apropos Ahithophel, the Gemara relates the events that led to his death. Rav Yehuda says that Rav says: A person should never bring himself to undergo an ordeal, as David, king of Israel, brought himself to undergo an ordeal and failed. David said before God: Master of the Universe, for what reason does one say in prayer: God of Abraham, God of Isaac, and God of Jacob, and one does not say: God of David? God said to David: They have undergone ordeals before Me, and you have not undergone an ordeal before Me. David said before Him: Examine me and subject me to an ordeal, as it is stated: “Examine me, Lord, and subject me to an ordeal; try my kidneys and my heart” (Psalms 26:2). God said to him: I will subject you to an ordeal, and I will perform a matter for you that I did not perform for the Patriarchs, as for them, I did not inform them of the nature of the ordeal, while I am informing you that I will subject you to an ordeal involving a matter of a married woman, with whom relations are forbidden. Immediately, it is written: “And it came to pass one evening that David rose from his bed” (II Samuel 11:2). The verse states: “And he walked upon the roof of the king’s house; from the roof he saw a woman bathing, and the woman was very fair to look upon” (II Samuel 11:2). Bathsheba was shampooing her head behind a beehive, which concealed her from sight. Satan came and appeared to David as a bird. David shot an arrow at the bird, the arrow severed the beehive, Bathsheba was exposed, and David saw her. Immediately, it is written: “And David sent and inquired after the woman. And one said: Is not this Bathsheba, daughter of Eliam, the wife of Uriah the Hittite? And David sent messengers, and took her, and she came to him, and he lay with her, for she was purified from her impurity, and then she returned to her house” (II Samuel 11:3–4). And that is the meaning of that which is written: “You have proved my heart; You have visited me in the night: You have tried me, but You find nothing; let no presumptuous thought pass my lips” (Psalms 17:3). David said: Oh, that a muzzle would have fallen upon the mouth of the one who hates me, a euphemism for his own mouth, and I would not have said anything like that and I would have withstood the ordeal. Rava taught: What is the meaning of that which is written: “For I am ready to stumble [letzela] and my pain is always before me” (Psalms 38:18)? Bathsheba, daughter of Eliam, was designated as fit for David from the six days of Creation. Rava interprets that the term letzela is referring to Eve, who was taken from the side [tzela] of Adam, the first man, and explains that she was destined for him, just as Eve was destined for Adam. But she came to him through pain. And likewise, the school of Rabbi Yishmael taught: Bathsheba, daughter of Eliam, was designated as fit for David, but he partook of her unripe, before the appointed time. David would have ultimately married her in a permitted manner after the death of Uriah.