标签:
一、在src目录下面建立一个test.properties的测试文件
1 id=0001 2 username=root 3 password=123456 4 url=http\://localhost\:1306/spring
二、新建一个PropertiesTest.java的测试类
1 package org.properties.test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Enumeration; 6 import java.util.Properties; 7 8 import org.junit.Test; 9 10 public class PropertiesTest { 11 12 /** 13 * @param args 14 */ 15 @Test 16 public void test() { 17 18 Properties pps = System.getProperties(); 19 pps.list(System.out); 20 } 21 22 @Test 23 public void test02() throws IOException { 24 25 InputStream is=PropertiesTest.class.getClassLoader().getResourceAsStream("test.properties"); 26 27 Properties p=new Properties(); 28 p.load(is); 29 30 Enumeration enum1 = p.propertyNames();//得到配置文件的名字 31 while(enum1.hasMoreElements()) { 32 String strKey = (String) enum1.nextElement(); 33 String strValue = p.getProperty(strKey); 34 System.out.println(strKey + "---" + strValue); 35 } 36 } 37 }
三、运行结果
运行test();方法结果:
1 -- listing properties -- 2 java.runtime.name=Java(TM) SE Runtime Environment 3 sun.boot.library.path=C:\Program Files\Java\jdk1.7.0_79\jre... 4 java.vm.version=24.79-b02 5 java.vm.vendor=Oracle Corporation 6 maven.multiModuleProjectDirectory=$MAVEN_HOME 7 java.vendor.url=http://java.oracle.com/ 8 path.separator=; 9 java.vm.name=Java HotSpot(TM) 64-Bit Server VM 10 file.encoding.pkg=sun.io 11 user.script= 12 user.country=CN 13 sun.java.launcher=SUN_STANDARD 14 sun.os.patch.level= 15 java.vm.specification.name=Java Virtual Machine Specification 16 user.dir=E:\tools\MyEclipse\MyEclipse 10\Works... 17 java.runtime.version=1.7.0_79-b15 18 java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment 19 java.endorsed.dirs=C:\Program Files\Java\jdk1.7.0_79\jre... 20 os.arch=amd64 21 java.io.tmpdir=C:\Users\duchong\AppData\Local\Temp22 line.separator= 23 24 java.vm.specification.vendor=Oracle Corporation 25 user.variant= 26 os.name=Windows 8.1 27 sun.jnu.encoding=GBK 28 java.library.path=C:\Program Files\Java\jdk1.7.0_79\bin... 29 java.specification.name=Java Platform API Specification 30 java.class.version=51.0 31 sun.management.compiler=HotSpot 64-Bit Tiered Compilers 32 os.version=6.3 33 user.home=C:\Users\duchong 34 user.timezone= 35 java.awt.printerjob=sun.awt.windows.WPrinterJob 36 file.encoding=UTF-8 37 java.specification.version=1.7 38 user.name=duchong 39 java.class.path=E:\tools\MyEclipse\MyEclipse 10\Works... 40 java.vm.specification.version=1.7 41 sun.arch.data.model=64 42 java.home=C:\Program Files\Java\jdk1.7.0_79\jre 43 sun.java.command=org.eclipse.jdt.internal.junit.runner... 44 java.specification.vendor=Oracle Corporation 45 user.language=zh 46 awt.toolkit=sun.awt.windows.WToolkit 47 java.vm.info=mixed mode 48 java.version=1.7.0_79 49 java.ext.dirs=C:\Program Files\Java\jdk1.7.0_79\jre... 50 sun.boot.class.path=C:\Program Files\Java\jdk1.7.0_79\jre... 51 java.vendor=Oracle Corporation 52 file.separator=53 java.vendor.url.bug=http://bugreport.sun.com/bugreport/ 54 sun.cpu.endian=little 55 sun.io.unicode.encoding=UnicodeLittle 56 sun.desktop=windows 57 sun.cpu.isalist=amd64
运行test02();方法结果:
1 password---123456 2 url---http://localhost:1306/spring 3 id---0001 4 username---root
四、可以应用的场景: 编写一个通用的方法,在不修改程序的情况下,可以获取任何数据库的连接
解决方案:把数据库驱动Driver实现类的全类名、url、user、password 放入一个配置文件中,通过修改配置文件的方式实现和具体的数据库解耦。
1 public static Connection getConnection2() throws Exception{ 2 String driverClass = null; 3 String jdbcUrl = null; 4 String user = null; 5 String password = null; 6 7 // 读取类路径下的jdbc.properties文件 8 InputStream in =DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); 9 Properties properties = new Properties(); 10 properties.load(in); 11 driverClass = properties.getProperty("driver"); 12 jdbcUrl = properties.getProperty("jdbcUrl"); 13 user = properties.getProperty("user"); 14 password = properties.getProperty("password"); 15 16 Driver driver = (Driver)Class.forName(driverClass).newInstance(); 17 18 Properties info = new Properties(); 19 info.put("user", user); 20 info.put("password", password); 21 22 Connection connection = driver.connect(jdbcUrl, info); 23 return connection; 24 }
标签:
原文地址:http://www.cnblogs.com/geekdc/p/5233772.html