The Tanakh was written by uninspired men, and the scriptures contradict these men Cheow, as David rightly points out. Here are a few thoughts on this from a few articles I read up on.
David M wrote,
David was very aware of his sin that was pointed out to him by Nathan. David said to God; "Against thee only have I sinned." David knew full well that he had sinned twice and was guilty of murder and adultery and should have been put to death.
But first, It is imperative to note here that a vital principle concerning Biblical inspiration be recognized. The fact that an event is recorded in Scripture does not necessarily suggest that the Lord approved of it. It is a fundamental feature of inspired Scripture that it documents the sins and follies of both its heroes and villains alike, the good and the bad.
The truth is, any attempt to justify adultery by appealing to David is akin to the distorted perversions of rabbinical Judaism than to accurate biblical exegesis. The Jewish Talmud seeks to justify the adultery of David on the ground that every soldier, before going into battle, was required to grant his wife a divorce; according to that twist, Bathsheba was actually free (Edersheim, IV, p. 191).
Moreover, if Davidís marriage to Bathsheba is to be employed as a pattern for illustrating Godís marriage code under the law of Christ, then polygamy becomes permissible. History reveals that Bathsheba was only one of eight wives (in addition to a number of concubines) which the king had (1 Sam. 18:27; 25:42-43; 1 Chron. 3:2-5).
And, as noted scholar Dean Stanley observed: 'His crime itself had sprung from the lawless and licentious life, fostered by the polygamy which he had been the first to introduce into the monarchy Ö' (II, p. 195).
The Mosaic Law principle certainly was applicable in the David-Bathsheba affair. They committed adultery. Had the law of Moses been strictly executed, they both would have been put to death.
'And the man that committeth adultery with another manís wife, even he that committeth adultery with his neighborís wife, the adulterer and the adulteress shall surely be put to death' (Lev. 20:10)
Nevertheless, because of Godís forbearance, Nathan informed the king: 'Jehovah also hath put away thy sin; thou shalt not die' (2 Sam. 12:13).
"And God shall wipe away all tears from their eyes; and there shall be no more death, neither sorrow, nor crying, neither shall there be any more pain: for the former things are passed away" (Rev. 21:4).