Java Development Tips

Source: Internet
Author: User

1 Loading the configuration file

Write the configuration file config.properties in the SRC directory of the Common Java project (if MAVEN works in the project's Src/main/resources) directory

Config.properties

hibernate.dialect=org.hibernate.dialect.OracleDialectdriverClassName=  Oracle.jdbc.driver.OracleDriverjdbc_url=jdbc:oracle:thin:@127.0. 0.1:1521: orcljdbc_username=Wangjdbc_password=123456

Propertiestool.java

 Public classPropertiestool {PrivateProperties props =NULL;  Public voidLoadprop (String configpath) {InputStream InputStream= Object.class. getResourceAsStream (Configpath); Props=NewProperties (); Try{props.load (InputStream); } Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException ("couldn ' t find properties file,please check it."); }    }     Publicstring ReadValue (String key) {String value=NULL; if(NULL!=props) {Value=Props.getproperty (key); }        returnvalue; }}

Run the test file

1 Read config.properties under src root file

 Public Static voidMain (string[] args) {Propertiestool Proptool=NewPropertiestool (); String Configpath="/config.properties";        Proptool.loadprop (Configpath); String value= Proptool.readvalue ("Hibernate.dialect"); System. out. println ("value="+value);}

2 read config.properties in the Com/xinping/service directory under the SRC root directory

 Public Static voidMain (string[] args) {Propertiestool Proptool=NewPropertiestool (); String Configpath="/com/xinping/service/config.properties";        Proptool.loadprop (Configpath); String value= Proptool.readvalue ("Hibernate.dialect"); System. out. println ("value="+value); }

Java Development Tips

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.