If your ships aren't as good technology-wise, and your economy is stable, you could set funding to research and set all colonies to focus research. Depending on how many colonies you have, you could completely research one line of defense techs (shield, PD, or armor) in a few turns.
Once you got to where you want to be tech-wise, you can design a new ship. You then have a few options to get back ontop militarily:
1) Set production slider to 0% for some quick cash and upgrade on all your existing ships. Pay attention to how many weeks it'll take to upgrade them. If in a war, they might get destroyed before you finish upgrading.
2) Set production slider to 100% with 100% military focus, and build some new ships. Rush buy some if currently in a war. You can always lower the production slider down if need cash.
Playing with the production slider can stall your social production, so don't keep this up for too long. But it is helpful sometimes.
Also, if you research most of the diplomacy techs, and have more colonies than the AI, they sometimes will not attack. I think they're scared of your 'potential' or something. They become more tollerant of your influence. Haven't quite figured out the specifics. I've had games where I don't build any combat ships for well over 300 turns into the game on a huge gigantic map, and never had the AI attack me.
edit: Another thing that will help avoiding a war, is to trade some techs for the AI's Economic and Research Treaties. You don't want to give any of the AIs your treaties. Normally I'll trade all the 'Trade' techs, 'Soil Enhancement', and a few minor techs for one, and the life-support and sensor techs for the other. Because the AI gave you the treaty, it doesn't want the bad relations for the rest of the game it'd get if it declared war and broke the treaty. I think this alone, with all 9 major AIs and 8 minor AIs giving me their treaties, plays a major part in allowing me to go 300+ turns without a single military vessel.
edit2: Started a new game today, and seems I was playing on gigantic maps instead of huge.