There are very simple lessons to learn and I think of them every time I read the narrative of this sorry episode in David's life.
1. Am I where I ought to be?
2. Am I looking at things I shouldn't be looking at?
3. Am I serving only myself?
4. Am I covering up my errors?
5. Is God going to be happy with what I'm about to do?
I'm sure there are more questions that come from the story, but I wonder what David would have done had he stopped at any of the first three? Had he been elsewhere the opportunity would not have arisen, had he looked elsewhere he would not have been tempted, had he accepted that Bathsheba was another man's wife, he would not have got himself into the mess he did.
Ah well, hindsight, as they say, is the only exact science.