Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:2:251

So they defeated them by permission of Allah, and David killed Goliath, and Allah gave him the kingship and prophethood and taught him from that which He willed. And if it were not for Allah checking ˹some˺ people by means of others, the earth would have been corrupted, but Allah is full of bounty to the worlds.  

And they routed, they broke, them, by the leave of God, by His will, and David, who was among the ranks of Saul’s army, slew Goliath; and God gave him, David, the kingship, over the Children of Israel, and Wisdom, prophethood after the death of Samuel and Saul, and the combination ˹of kingship and prophethood˺ had never come to anyone before him; and He taught him such as He willed, of the manufacture of mail-coats and the speech of birds. Had God not repelled people some (ba‘dahum, ‘some’, substitutes for ˹saying˺ ba‘dan min al-nās, ‘some people’) by means of others the earth would have surely been corrupted, with idolaters defeating and slaying Muslims, and mosques being destroyed; but God is bounteous to all worlds, by repelling some by means of others.
القرآن:٢:٢٥١

فَهَزَمُوهُمْ بِإِذْنِ اللَّهِ وَقَتَلَ دَاوُودُ جَالُوتَ وَآتَاهُ اللَّهُ الْمُلْكَ وَالْحِكْمَةَ وَعَلَّمَهُ مِمَّا يَشَاءُ ۗ وَلَوْلَا دَفْعُ اللَّهِ النَّاسَ بَعْضَهُمْ بِبَعْضٍ لَفَسَدَتِ الْأَرْضُ وَلَٰكِنَّ اللَّهَ ذُو فَضْلٍ عَلَى الْعَالَمِينَ  

{فهزموهم} كسروهم {بإذن الله} بإرادته {وقتل داود} وكان في عسكر طالوت {جالوت وآتاه} أي داود {الله الملك} في بني إسرائيل {والحكمة} النبوة بعد موت شمويل وطالوت ولم يجتمعا لأحد قبله {وعلّمه مما يشاء} كصنعة الدروع ومنطق الطير {ولولا دفع الله الناس بعضهم} بدل بعض من الناس {ببعض لفسدت الأرض} بغلبة المشركين وقتل المسلمين وتخريب المساجد {ولكنّ الله ذو فضل على العالمين} فدفع بعضهم ببعض.