This particular event happened after chapters in 2 Samuel detailing how David had subdued in battle other peoples and enemies around Israel, and how God was the one who established everything and kept David safe in all his victories.
After all that we find David staying home from battle (2 Samuel 11). We aren’t told the reason, just that he had stayed home. While he was resting one evening, he decided to get up and walk around the roof. While walking he happened to see a woman bathing. Now the Bible doesn’t normally tell us what people look like, so when the Bible takes a moment to say this woman was beautiful, she must have been a knockout. This is probably also part of the reason why David decided to find out who she was instead of leaving the matter alone. He found out her name was Bathsheba and that she was the wife of one of his soldiers, Uriah, who was currently out fighting in battle.
At the time, it was acceptable in most kingdoms for the king to do whatever he wanted with whoever he wanted. But God held the children of Israel and the king thereof to much higher standards than the cultures around them. Sill, David sent to invite Bathsheba over. Whatever her reason, she accepted. They had sex, and Bathsheba went home.
Bathsheba soon found she was pregnant, so she sent a message to David to tell him. So David decided he’d try to cover things up. He sent to his trusted captain and asked him to send Uriah. When Uriah came David asked him questions about how the military and battle were doing, as if that’s the only reason why Uriah was there. Afterwards David told Uriah to go spend some time at home before going back to the battlefield, hoping that he’d have sex with his wife, therefore making the child look like his.
But Uriah didn’t go home. He slept by David’s house with the servants. David got word of this, and judging by the use of “they told him,” a lot of people probably knew what was going on. So David asked Uriah why he hadn’t gone home. Uriah said that it wasn’t right for him to go home and enjoy those comforts while all his fellow soldiers were out camping in the open fields. So David had Uriah wait another day before he sent him back to the field, and had Uriah eat with him. David got Uriah drunk and hoped then he’d go home. But he still didn’t, and I’ve heard someone put it this way: “At this point, Uriah was a better man drunk than David was sober.”
Still trying to cover his sin, David wrote a message to the captain saying to get Uriah killed in battle. He then sent this message with Uriah himself. For whatever reason the captain obeyed, aiding in the murder of an innocent man. Messages were exchanged between David and the captain about the deed being done, and David tried to pretend that Uriah’s death couldn’t have been helped.
Bathsheba found out that her husband had died, though I don’t know if she knew how and why. She mourned for him, and when she was finished with her mourning David sent to her again. I don’t know what he said, but she came to his house and became his wife, one of several already. In the last verse of 2 Samuel 11, we see that God was very unhappy with the whole ordeal.
So in chapter 12 God sent Nathan the prophet to David to tell him what he had done was absolutely wrong, using a story to illustrate the point. For such a set of crimes there had to be punishment. God told David that there would be violence and strife throughout his lifetime, detailing from whence some of it would come. He told David that this sin was visible to the enemies, giving them cause to blaspheme or scorn God. He also told David that the son to which Bathsheba had given birth would die.
After that, David’s and Bathsheba’s son got very, very sick. David pleaded with God for his son, laying all night on the ground and refusing to eat or leave his place. He was so distraught that when his son finally died, his servants were afraid to tell him, thinking he might just die of grief. But he saw their whispering and asked them, so they told him. So he got up and washed, changed his clothes, went to God’s house, and worshipped God. Then he went home and ate. His servants were confused, so he explained that while his son was alive he had hope that God might spare him, but now that he was gone there was no use in his fasting. He knew that his son wouldn’t be coming back, but he also knew his son had gone to Heaven and he would eventually go to see him.
So now we come to the part about forgiveness. God had to punish David and Bathsheba, but that didn’t mean He couldn’t forgive them. They had another son and named him Solomon, and that was the son that God blessed and chose to rule when David was gone. That was the son that built the temple that David had wanted to build for God.