原来的程序把连接数据库的代码都写在了JAVA代码里,这样在别的服务器里部署在tomcat后,想修改USER PASSWORD URL等信息非常麻烦,还是应该把这些属性写在Properties文件中比较方便。
package util;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesTest {
public static void readProperties(String fileName) {
try {
Properties props = new Properties();
/*
String fileName1 = "src/util/oracle.properties";
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(fileName));*/
// String fileName = "oracle.properties";
InputStream in = PropertiesTest.class.getResourceAsStream(fileName);
props.load(in);
Enumeration enu = props.propertyNames();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
String property = props.getProperty(key);
System.out.println(key + "=" + property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "db.properties";
readProperties(fileName);
}
}
db.properties
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
sysUrl=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Monitor_Sys
dataUrl=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Monitor_Data
user=sa
password=123