Despite being "a man after God's own heart," David was very human with all of the flaws and weaknesses that can be found in any human being. He famously sinned with Bathsheba, impregnated her and had her husband killed when he refused to go into her during a weekend pass designed to cover David's tracks. Despite his contrition and repentance and God's forgiveness, there were consequences for his actions. One of the consequences unfortunately involved the death of the child. David then experienced a depth of sorrow that every parent dreads. The illness and expected passing of his baby made him inconsolable.
Beloved, I pray that you will never know such loss, particularly as a result of your own sin. Nevertheless, we all, at some point in our lives, have known or will know despair. If we haven't yet, we will someday experience pain that is unimaginable. Despite the best efforts of others to comfort us, there seems to be no word that can be spoken, no medicine that can be taken and no prayer that can be prayed that will deliver us from our sorrow. This is a normal part of our human existence. The key is to remain faithful despite these trials and tribulations.
When there was nothing more to be said or done, David cleaned himself up and went to worship. What we learn from him is that despite his sin, punishment and pain, he never lost contact with the God who made Him. He remained prayerful throughout his ordeal. Through fasting, he sacrificed to the Lord hoping to find favor. And when he found that God's judgment was final, he worshiped him. He never broke faith with God nor lost faith in Him despite having the worst experience of his life. When his servants asked why he despaired so when the child was still alive but recovered so quickly upon his death, he shared concerning his child, "I will go to him, but he will not return to me," 2 Samuel 12:23. Even this was an amazing display of faith in the Father. When storms come, let us all be sure to do likewise.