1 Importjava.io.FileNotFoundException;2 Importjava.io.IOException;3 ImportJava.io.InputStream;4 Importjava.sql.SQLException;5 Importjava.util.Properties;6 7 ImportJavax.sql.DataSource;8 9 Importcom.alibaba.druid.pool.DruidDataSourceFactory;Ten One Public classjdbcutils A { - protected StaticDataSource ds; - the Static - { - Try - { +Properties Properties = Loadpropertyfile ("Druid.properties"); -DS =Druiddatasourcefactory.createdatasource (properties); + } A Catch(Exception e) at { - e.printstacktrace (); - } - } - - Public StaticProperties loadpropertyfile (String fileName) in { - if(NULL= = FileName | | Filename.equals ("")) to { + Throw NewIllegalArgumentException ("Properties file path can not is null:" +fileName); - } the *InputStream InputStream =NULL; $Properties Properties =NULL;Panax Notoginseng Try - { theInputStream = Jdbcutils.class. getClassLoader (). getResourceAsStream (fileName); +Properties =NewProperties (); A properties.load (inputstream); the } + Catch(FileNotFoundException e) - { $ Throw NewIllegalArgumentException ("Properties File not found:" +fileName); $ } - Catch(IOException e) - { the Throw NewIllegalArgumentException ("Properties file can not loading:" +fileName); - }Wuyi finally the { - Try Wu { - if(InputStream! =NULL) About { $ inputstream.close (); - } - } - Catch(IOException e) A { + e.printstacktrace (); the } - } $ returnproperties; the } the the /** the * Get Data source - * in * @return the */ the Public StaticDataSource Getdatasource () About { the returnds; the } the + Public Static voidCloseConnection ()throwsSQLException - { the if(ds! =NULL&& ds.getconnection ()! =NULL&&!ds.getconnection (). isClosed ())Bayi { the ds.getconnection (). Close (); the } - } -}
Java reads the properties file in a Web project