David, as a good shepherd is a figure of Christ in the Old Testament. When still very young, he looked after the flock of sheep of his father, and learned to cultivate the virtues necessary to be a good shepherd. In this, God prepared him to be the king of Israel later, and to look after that flock of His. It was the same apprenticeship through which Moses and the prophet Amos went.
After being anointed and acclaimed king, David expelled the enemies who oppressed the people, gained a capital for them, Jerusalem, where he consolidated the worship of God in the tabernacle, took the ark of the covenant back to it with great jubilation and instituted order in the ranks of priests and Levites, choirs and instrumental music. Being himself endowed with great poetic and musical talent, he composed a great part of the psalms (literally: a striking, twanging), including the renowned Psalm 23 which treats the Lord as the Good Shepherd.
His faithfulness to the LORD the God of Israel, and to the fulfilment of His laws and precepts was exemplary, to the point of being the high standard against which all his descendants on the throne of Judah were measured.
Although he was a man according to the heart of God, he stumbled a few times, and two of his sins, revealed in the Bible, stick out because of the dire consequences they brought to him.
The first (2 Samuel 11) shocks us because of the immorality of David and astonishes us with the solution he found in the death of Uriah. In the eyes of God, however, the greater seriousness was the injustice done by David. This injustice was illustrated in the parable of the prophet Nathan (2 Samuel 12:1-4). David was furious with the rich man in the parable saying that he deserved to die for having killed the only pet lamb of the poor man (note the object - a lamb - which would be the most touching to the heart of a shepherd). When Nathan pointed out that David had done the same as the rich man, when he arranged for Uriah to die and to take his wife to himself, he realised the wickedness of what he had done, confessed his sin and the Lord forgave him. But the consequences came later on with the scandal promoted by his son Absalom, and the death of the child he had with the wife of Uriah. It is one of the examples where we learn that, although God pardons the repentant sinner, ill effects may still result from his sin.
His second sin (2 Samuel 24 and 1 Chronicles 21) was a lot more serious and devastating in its consequences, and is evidenced to a greater extent because it is also mentioned in the book of Chronicles.
To answer the present question we must refer to these two chapters. In the first verses, we find what seems to be a discrepancy: "Again the anger of the LORD was aroused against Israel, and He moved David against them to say, "Go, number Israel and Judah.'" (2 Samuel 24:1). If David was obeying an order of the LORD, how could he be sinning? We read, however, in 1 Chronicles 21:1: "Now Satan stood up against Israel, and moved David to number Israel." Combining the two, we discover that: "… the anger of the LORD was aroused against Israel, and…" (with His permission) "… Satan stood up against Israel, and moved David to number Israel" (which included Judah at that time). We therefore understand that:
The anger of the LORD once again flared against Israel: the reason isn't given, nor when, but it is certain that the children of Israel as a whole was seriously at fault and needed correction.
To punish Israel, the LORD used Satan to tempt their king. Satan couldn't touch Israel without the permission of God, but had the sagacity to see that through their king David he could subvert the people. So Satan instigated David, we know not how, to take a national census.
David was tempted and Satan must have observed that, at that juncture, David apparently had two weak points: self-satisfaction and lack of confidence in God. There was great subtlety in the temptation, for the act of taking a census wasn't sinful provided it was in the will of God.
All indicates that the motive for the census was David's desire to know the size of his effective army, to evaluate his combative power, forgetting that the victories were obtained only by the grace and power of God.
"Thus says the LORD: 'Let not the wise man glory in his wisdom, Let not the mighty man glory in his might, Nor let the rich man glory in his riches; but let him who glories glory in this, that he understands and knows Me, that I am the LORD, exercising lovingkindness, judgment, and righteousness in the earth. For in these I delight," says the LORD." (Jeremiah 9:23-24). The LORD wasn't delighted when David ordained the census because David wasn't glorying in the LORD but in his own strength. His motive was the sin of unbelief. He was trusting in numbers instead of trusting in God.
The LORD had given instructions to Moses concerning the census of the people: when carried out, all those who were numbered were to give half a shekel as an offering to the LORD, that there might be no plague among them (Exodus 30:11-15). We see no mention of this here and it is possible that it may have been forgotten.
David had consulted the LORD so many times in the past concerning important actions he needed to take and the LORD had given him the reply. This time he didn't consult the LORD, even after Joab tried to change his mind, perceiving the sinful motive of David.
Unable to dissuade David, Joab carried out the census, but he did not count Levi and Benjamin among them, for the king's word was abominable to Joab. He only counted the men who drew the sword.
God showed His displeasure by striking Israel. Then David saw the extent of his sin, and asked God to take away his iniquity for he had done very foolishly. But it was too late. Among the three punishments the LORD proposed to him, David opted to fall into the hand of the LORD, for His mercies are very great. But the LORD sent a plague upon Israel but held it back in Jerusalem where, on instructions of the Angel of the Lord, David offered burnt offerings and peace offerings, and called on the LORD.
In conclusion, we see that it all began because God was angry with Israel, so the people were punished when its cause was discovered: it was the self-satisfaction and unbelief towards God displayed by their king. Although David didn't suffer physically, he was anguished because of the suffering of the people, for a sin he attributed to himself: "Was it not I who commanded the people to be numbered? I am the one who has sinned and done evil indeed; but these sheep, what have they done? Let Your hand, I pray, O LORD my God, be against me and my father's house, but not against Your people that they should be plagued." It was the anguished cry of the shepherd feeling responsible for the suffering of his sheep.
We should never trust in statistics to verify "our" spiritual victory, "our" material and spiritual resources, the number of "our" churches, in order to rejoice over "our" efforts. This testifies against our dependence on the Lord of the harvest, and makes us trust in our own strength: "Unless the LORD builds the house, they labour in vain who build it; unless the LORD guards the city, the watchman stays awake in vain." (Psalm 127:1). How dangerous it is to make of ministry an idol, and to turn the work more important than the Lord of the harvest. How often, in overseers' meetings in our churches, we fail to mention material and spiritual blessings received and involve ourselves entirely in checking the funds in the treasury, or the number of baptisms during the period and how many more members we acquired. If the figures please us, we think it was a great spiritual victory we won whilst, on the contrary, it might constitute the worst thing possible that could happen: the church leaving its spiritual nature to become a club of the flesh.
David knew that he would be well in the hand of the LORD. Let us also trust Him when we go through our trials. "For whom the LORD loves He chastens, and scourges every son whom He receives." (Hebrews 12:6).