View Javadoc
1 /* 2 * StartUpImpl.java 3 * 4 * Created on February 20, 2003, 10:14 PM 5 */ 6 7 package com.mlw.fps.model.business.dao.hibernate; 8 9 import java.util.List; 10 import java.util.Properties; 11 12 import net.sf.hibernate.Query; 13 import net.sf.hibernate.Session; 14 import net.sf.hibernate.SessionFactory; 15 import net.sf.hibernate.cfg.Configuration; 16 17 import com.mlw.fps.model.business.bean.SystemProperties; 18 import com.mlw.fps.model.business.dao.StartUp; 19 20 /*** 21 * 22 * @author Matthew Wilson 23 */ 24 public class Config implements StartUp 25 { 26 private boolean initialized = false; 27 private static SessionFactory sessions; 28 29 /*** Creates a new instance of StartUpImpl 30 */ 31 public Config() 32 { 33 } 34 35 public static SessionFactory getSessionFactory() 36 { 37 return sessions; 38 } 39 40 public boolean init() 41 { 42 Properties props = new Properties(); 43 44 props.setProperty("hibernate.dialect","net.sf.hibernate.dialect.MySQLDialect"); 45 props.setProperty("hibernate.connection.driver_class","org.gjt.mm.mysql.Driver"); 46 props.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver"); 47 props.setProperty("hibernate.connection.url","jdbc:mysql:///fpsstats"); 48 props.setProperty("hibernate.connection.username","root"); 49 props.setProperty("hibernate.connection.password",""); 50 51 try 52 { 53 Configuration cfg = new Configuration().setProperties(props) 54 .addClass(Config.class) 55 ; 56 57 sessions = cfg.buildSessionFactory(); 58 59 60 String logDir = SystemProperties.getProperty("logDirectory"); 61 new com.mlw.fps.load.adapter.HalfLifeLoader(logDir).load(); 62 63 } 64 catch(Exception e) 65 { 66 e.printStackTrace(); 67 } 68 69 return (initialized=true); 70 } 71 72 public boolean isInitialized() 73 { 74 return initialized; 75 } 76 77 78 79 80 //Utility Methods 81 public static void save(Object object) 82 { 83 Session session = null; 84 85 try 86 { 87 session = sessions.openSession(); 88 session.saveOrUpdate(object); 89 } 90 catch(Exception e) 91 { 92 e.printStackTrace(); 93 } 94 finally 95 { 96 try 97 { session.flush(); session.close(); } catch(Exception e) 98 {} 99 } 100 } 101 102 public static Object load(Class klass, String pk) 103 { 104 Session session = null; 105 106 try 107 { 108 session = sessions.openSession(); 109 return session.load(klass, pk); 110 } 111 catch(Exception e) 112 { 113 e.printStackTrace(); 114 return null; 115 } 116 finally 117 { 118 try 119 { session.flush(); session.close(); } catch(Exception e) 120 {} 121 } 122 } 123 124 public static Object findByName(String query, String name) 125 { 126 Session session = null; 127 128 try 129 { 130 session = sessions.openSession(); 131 Query q = session.getNamedQuery(query); 132 q.setString(0, name); 133 134 java.util.List results = q.list(); 135 if(results.size()>0) 136 { 137 return results.get(0); 138 } 139 else 140 { 141 return null; 142 } 143 } 144 catch(Exception e) 145 { 146 e.printStackTrace(); 147 return null; 148 } 149 finally 150 { 151 try 152 { session.flush(); session.close(); } catch(Exception e) 153 {} 154 } 155 } 156 157 public static List query(String query, String[] parms) 158 { 159 Session session = null; 160 161 try 162 { 163 session = sessions.openSession(); 164 Query q = session.getNamedQuery(query); 165 if( parms!=null ) for(int i=0; i<parms.length; i++) 166 { 167 q.setString(i, parms[i]); 168 } 169 170 return q.list(); 171 } 172 catch(Exception e) 173 { 174 e.printStackTrace(); 175 return null; 176 } 177 finally 178 { 179 try 180 { session.flush(); session.close(); } catch(Exception e) 181 {} 182 } 183 } 184 185 }

This page was automatically generated by Maven