David was not planning to commit adultery. In fact, he was so upright he wouldn't even think of it. We know that even when he appreciated Abigail, the wife of Nabal, he kept away until her husband died (1Sa.25:39). But he just got excited one day when he unexpectedly saw Bathsheba bathing, called for her and slept with her (2Sa.11:2-4). He sent her home after this, possibly thinking that this was the end of the matter. But she sent word saying she had become pregnant. So he called for Uriah, her husband, and tried his best to make it look as if Uriah was responsible for the baby. But when that man didn't play along, David arranged to have him killed. He thought that no one other than Joab his army chief would know. But God was watching.
We know David was earlier such an upright man that even when opportunity came up twice to finish off King Saul who was out to kill him, he refrained (1Sa.24:6;26:9). Hardly any man would have criticised him had he killed Saul then, because God had already anointed him as king and Saul was troubling him even though he was only trying to serve him. But he was caught unprepared in the case of Bathsheba, and he thought he was 'forced' to kill Uriah in order to preserve his kingdom and his reputation as a man after God's own heart!
We see how King Herod who was scared to kill John the Baptist because he was a prophet and actually liked to listen to him (Mk.6:20) suddenly found himself in a situation where he had to order to kill him! He wanted to avoid becoming embarrassed in front of his guests (v.26)!
Can we assume, "I'll never do anything like that!"? Can we afford to look down on others who fall? "Therefore let him who thinks he stands take heed that he does not fall" (1Co.10:12). Godly people look at the 'worst' examples of sinners and say to themselves, "But for the grace of God, there go I." God preserves the humble. But let us walk with trembling because God will certainly oppose us if we begin to be proud (Jas.4:6). If He removes the protection of grace over us, the best of us can fall down to the deepest depths.
Another lesson that we need to learn is that major falls into sin do not happen just like that, suddenly, out of the blue. It is the little falls here and there beginning in our attitude and thoughts that slowly prepare us for the major falls. We can be sure that our enemy, the Devil, is watching us all the time, and biding the time to catch us off guard (1Pe.5:8). We can't think we are safe by arguing that we aren't really doing anything seriously wrong, because the Devil knows our weak spots and is always waiting for the right moment.
God wants us to work out our salvation with fear and trembling (Php.2:12). Of course, God is watching over us to protect us. But when pride comes, it comes along with the companion called carelessness. That is why we need to work on maintaining an attitude of humility, a sense of dependence on God, and then be sober and vigilant.