Not all types of prostatitis are preventable.
You can prevent infections caused by STDs by practicing safer-sex behaviors.
Barry MJ, McNaughton-Collins M. Benign Prostate Disease and Prostatitis. In: Goldman L, Ausiello D, eds. Cecil Medicine. 23rd ed. Philadelphia, Pa: Saunders Elsevier; 2007:chap 130.