The other answers on this thread are terrific, so I just want to add my 2 cents.
This is a great question and, quite honestly, is something that I've dealt with before and still do sometimes today.
I've been in quite a few situations in which I've found myself losing confidence when something negative happens, e.g. doing poorly on a midterm (or a whole class), competitive sport, or interview. It usually came accompanied by thoughts like "I'm not qualified," "I'm not smart enough," and "why do I bother?". And it sucked because I became even more fearful of failure the next time around, which would make me more nervous.
A couple years ago, I read a book that completely changed my perspective on this. It's called Mindset by Carol Dweck. At a high level, it talks about 2 mindsets.
The first, the "Fixed Mindset", is one in which you believe that talent is innate and 'fixed'. If you're good at something, then great! You're gifted! If you fail at something, then there's no use trying to improve because there's only so much you can do.
The second is called the "Growth Mindset." Those with a growth mindset believe that ability is not innate and that, with hard work, they can improve. They dedicate themselves to their goals.
I'd encourage you to think more about the Growth Mindset and try to adopt it. You're bound to make mistakes, but it's what you do with them that makes the difference. Examples: fail a midterm? You could grab a tub of ice cream and binge watch Netflix. OR you could go to office hours for your professor and/or TA with your midterm, solve the problems you got wrong, and find out where you can improve for your final. Get rejected from your dream company? You could slink away embarrassed and lower your standards. OR you could ask the hiring manager and/or recruiter about what you can improve on, and what they're looking for in an ideal candidate, and spend the next couple months improving so that you're better prepared when you reapply.
As a personal example, I started looking for a new job last year because I wanted to move cities and try something new. From previous experience, I've always known that I struggled with nerves and confidence during interviews, which would cause me to not think straight and not convey my thoughts properly. And if I had interviewed right away, I'm pretty sure I would have bombed them. After thinking about it, I realized a lot of my confidence issues stemmed from not feeling prepared. So I set goals. I studied on weekdays before and after work, and all day on weekends. For a long time. I reached out to friends to help me prep. And when the time came to interview, I walked into interviews knowing that I did the best I could up until this point. If they didn't go well, I'd see where I could improve and try again the next time. One of the first ones didn't go very well, but it helped me realize an area that I needed to brush up on. I ended up getting offers from 5 of the top tech companies in the Bay Area.
Mistakes are the stepping stones to success. When they happen, keep your head held high and treat mistakes as learning opportunities, because they may teach you more than your successes will.
Best of luck and go Bruins!
BTW - Carol's also given a Ted Talk on the Growth Mindset. It's a great video: https://www.youtube.com/watch?v=_X0mgOOSpLU