First, create a test.properties test file under the SRC directory
1 id=0001 2 username=root 3 password=123456 4 url=http\:// localhost\:1306/spring
Second, a new Propertiestest.java test class
1 Packageorg.properties.test;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.util.Enumeration;6 Importjava.util.Properties;7 8 Importorg.junit.Test;9 Ten Public classPropertiestest { One A /** - * @paramargs - */ the @Test - Public voidTest () { - -Properties pps =system.getproperties (); + pps.list (System.out); - } + A @Test at Public voidtest02 ()throwsIOException { - -InputStream is=propertiestest.class. getClassLoader (). getResourceAsStream ("Test.properties"); - -Properties p=NewProperties (); - P.load (IS); in -Enumeration enum1 = P.propertynames ();//get the name of the configuration file to while(Enum1.hasmoreelements ()) { +String strkey =(String) enum1.nextelement (); -String strvalue =P.getproperty (strkey); theSystem.out.println (strkey + "---" +strvalue); * } $ }Panax Notoginseng}
Iii. Results of operation
Run test (); Method Result:
1--Listing properties--2Java.runtime.name=Java (TM) SE Runtime Environment3Sun.boot.library.path=c:\program files\java\jdk1.7. 0_79\jre .4java.vm.version=24.79-B025Java.vm.vendor=Oracle Corporation6maven.multimoduleprojectdirectory=$MAVEN _home7Java.vendor.url=http://java.oracle.com/8Path.separator=;9Java.vm.name=java HotSpot (TM) 64-Bit Server VMTenfile.encoding.pkg=Sun.io Oneuser.script= Auser.country=CN -Sun.java.launcher=Sun_standard -Sun.os.patch.level= theJava.vm.specification.name=Java Virtual Machine Specification -User.dir=e:\tools\myeclipse\myeclipse 10\works ... -java.runtime.version=1.7.0_79-B15 -java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment +Java.endorsed.dirs=c:\program files\java\jdk1.7. 0_79\jre . -Os.arch=AMD64 +Java.io.tmpdir=C:\Users\duchong\AppData\Local\Temp ALine.separator= at -Java.vm.specification.vendor=Oracle Corporation -User.variant= -Os.name=windows 8.1 -sun.jnu.encoding=GBK -Java.library.path=c:\program files\java\jdk1.7. 0_79\bin . inJava.specification.name=Java Platform API specification -Java.class. version=51.0 toSun.management.compiler=hotspot 64-Bit Tiered Compilers +os.version=6.3 -User.home=C:\Users\duchong theUser.timezone= *java.awt.printerjob=Sun.awt.windows.WPrinterJob $File.encoding=utf-8Panax Notoginsengjava.specification.version=1.7 -User.name=Duchong theJava.class. path=e:\tools\myeclipse\myeclipse 10\works ... +java.vm.specification.version=1.7 ASun.arch.data.model=64 theJava.home=c:\program files\java\jdk1.7. 0_79\jre +Sun.java.command=Org.eclipse.jdt.internal.junit.runner ... -Java.specification.vendor=Oracle Corporation $User.language=ZH $awt.toolkit=Sun.awt.windows.WToolkit -java.vm.info=Mixed Mode -java.version=1.7. 0_79 theJava.ext.dirs=c:\program files\java\jdk1.7. 0_79\jre . -Sun.boot.class. Path=c:\program files\java\jdk1.7. 0_79\jre .WuyiJava.vendor=Oracle Corporation theFile.separator= -Java.vendor.url.bug=http://bugreport.sun.com/bugreport/ Wusun.cpu.endian=Little -sun.io.unicode.encoding=Unicodelittle Aboutsun.desktop=Windows $Sun.cpu.isalist=amd64
Run test02 (); Method Result:
1 Password---123456 2 URL---http://localhost:1306/spring 3 ID---0001 4 username---root
Four, can be applied scenario: write a common method, without modifying the program, you can get any database connection
Solution: The full class name, URL, user, and password of the database-driven driver implementation class are put into a configuration file, which is implemented by modifying the configuration file and decoupling the specific database.
1 Public StaticConnection GetConnection2 ()throwsexception{2String Driverclass =NULL;3String Jdbcurl =NULL;4String user =NULL;5String Password =NULL;6 7 //read the Jdbc.properties file under the Classpath8InputStream in =dbutil.class. getClassLoader (). getResourceAsStream ("Jdbc.properties");9Properties Properties =NewProperties ();Ten properties.load (in); OneDriverclass = Properties.getproperty ("Driver"); AJdbcurl = Properties.getproperty ("Jdbcurl"); -user = Properties.getproperty ("User"); -Password = properties.getproperty ("Password"); the -Driver Driver =(Driver) class.forname (Driverclass). newinstance (); - -Properties info =NewProperties (); +Info.put ("User", user); -Info.put ("Password", password); + AConnection Connection =Driver.connect (Jdbcurl, info); at returnconnection; -}
Java Read the properties file