David sought forgiveness, though. God gave it to him. 1 John 1:9 promises that if we confess what we’ve done to Him, “He is just and faithful” to forgive us. He will “cleanse us from all unrighteousness.” He did that for good when Jesus died on the cross.
However, we have to receive that forgiveness first. That is done is through simple faith. Only then can He cleanse us. It’s like David. God was waiting for him to confess his sin, and He would have forgiven him, before any worse stuff happened. However, David was too stubborn, and kept trying to cover his sin himself. That made his sin worse, and caused many more negative consequences, till he finally repented.