One verse says, "Shall you build?" and another verse says, "You shall not build" (I Chronicles 17:4). "You shall not build" — that you do not build it; "Shall you build?" — that it is called by your name. Rabbi Chunya in the name of Rabbi Shimon ben Lakish: David dug down one thousand five hundred cubits to stand upon the foundation of the earth. As it is taught: There are three virgins — the virgin of a human, the virgin of a sycamore, the virgin of the earth. The virgin of a human — any who has never been known. The virgin of a sycamore — any that has never been cut. The virgin of the earth — any that has never been worked (ever). Rabban Shimon ben Gamliel says: any in which there is no potsherd. And when David dug one thousand five hundred cubits, he found in it a potsherd. He said, "All this labor that I have labored, and afterward I have found a potsherd!" At that moment the Holy One, blessed be He, gave the potsherd an opening of the mouth, and it said, "This is not my place; rather, at the time when the earth was split open, I descended here. If you do not believe me, behold, the deep is set beneath me." David lifted it, and the deep rose up and flooded. He said, "Whoever knows how to say a word to make this deep stand still, and does not make it stand, let him be strangled." And Ahithophel was there, and he said a word and made it stand. Even so, he was strangled. This is what is written, "And he gave charge concerning his house, and strangled himself" (II Samuel 17:23). Rabbi Yose said: From here a person must be wary of the curse of an elder, even when it is uttered for nothing.
David Digs the Temple Foundations and the Restraining of the Deep
Midrash Shmuel 26:2
כתוב אחד אומר האתה תבנה וכתוב אחר אומר לא אתה תבנה (דה"א י"ז ד'), לא אתה תבנה שאין אתה בונה אותו, האתה תבנה שהוא נקרא על שמך, רבי חוניא בשם רבי שמעון בן לקיש אלף וחמש מאות אמות חפר דוד לעמוד על משתיתה של ארץ, דתניא שלש בתולות הן, בתולת אדם, בתולת שקמה בתולת הארץ, בתולת אדם כל שלא נבעלה מימיה, בתולת שקמה כל שלא נקצצה מימיה, בתולת הארץ, כל שלא נעבדה מימיה (מעולם), רבן שמעון בן גמליאל אמר כל שאין בה חרש, וכיון שחפר דוד אלף וחמש מאות אמה מצא בה חרש, אמר כל יגיעה זו שיגעתי ואח"כ מצאתי חרש, באותה שעה נתן הקדוש ברוך הוא פתחון פה לחרש ואמר אין זה מקומי, אלא בשעה שנבקעה הארץ ירדתי כאן אם אין את מאמיני הרי תהום נתון תחתי, נטלו דוד ועלה תהום והציפו, אמר כל מאן דידע למימר מילה (מימר) למיקמה הדין תהומא, ולא מקים ליה יתחנק, והוה תמן אחיתופל ואמר מילה (ואנקמי') [ואוקמיה], אפילו כן אתחנק הדא הוא דכתיב ויצו אל ביתו ויחנק (שמואל ב' י"ז ב'), אמר רבי יוסי מיכן צריך אדם לחוש על קללתו של זקן (יותר) אפילו על חנם.