Java reads the properties file in a Web project

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.