Scripture clearly says that he was forgiven for that sin, along with his adultery. But... it also clearly says that his son died because of David's contempt of G_d, as in Nathan's words. A life for a life (or you could say: a life for a wife!). Pharoah's son also died because of his father's contempt for G_d, along with all the first born in Egypt - a lesson that should have been learned by G_d's chosen people, whose ancestors had been guilty of the same sin in the desert, and a whole generation lost out on seeing the promised land.
Achan's sin showed that contempt of G_d causes not only the one person to die, but his whole family and that sin can injure the nation as well. Ezekiel points the way forward - the way of personal sin and responsibility.
I guess G_d could stop all sin, but that would not be free will, would it? G_d knew David's heart, that if the man had survived that battle, he would have been murdered another way.
If you show the other bits of scripture to which you refer they, too, can be addressed.