The Book of 2 Samuel does not tell us who wrote it. Most likely the Prophet Samuel did not write it because he died in 1 Samuel. Nathan and Gad are considered as two possible authors of this book. It was written during the time of King David, so the book basically chronicles about him, especially his conquests and troubles.
Here the books exposes David’s sins. First he commits adultery with a married woman named Bathsheba, and their affair results in her pregnancy. As if that wasn’t wrong enough, David has her husband placed in a position in battle that he is sure to be killed. The prophet Nathan confronts David with his sin. He confesses to God and asks for His forgiveness. God does forgive David graciously, but warns him that trouble would arise as a result of his sins.
First, his first child with Bathsheba dies. She later gives birth to Solomon, who will someday be the future King of Israel. David’s other son Absalom plots a revolt against his own father. David flees from Jerusalem, but raises a large enough number of troops in his bid to restore his kingdom. However, this is not without bloodshed. And more tragically, his son is killed in the process, and David mourns for him.
From King David’s life, we learn that even the most perfect and the most blessed people can fall into temptation and commit sins. God is gracious and will forgive us even if we have committed the most heinous transgressions. However, being forgiven does not mean that the scars of our sins would be erased — and it is possible that trouble could ensue as a consequences of our sins. It is better to ask for forgiveness because of the sins committed, but it is best to not to commit them in the first place.