网站是软件吗,网络广告营销的好处,平面设计主要做的是什么,网站备案 主办单位文章目录创建属性文件解析属性文件获取数据使用类加载器使用 File 对象创建属性文件
新建 db-oracle.properties , 存放项目必须使用到的参数#xff1a;
driver oracle.jdbc.driver.OracleDriver
url jdbc:oracle:thin:192.168.0.23:1521:htlwk
username openlab
passwo…
文章目录创建属性文件解析属性文件获取数据使用类加载器使用 File 对象创建属性文件
新建 db-oracle.properties , 存放项目必须使用到的参数
driver oracle.jdbc.driver.OracleDriver
url jdbc:oracle:thin:192.168.0.23:1521:htlwk
username openlab
password open123解析属性文件获取数据
使用类加载器
public class ConnectionUtils {private static String driver;private static String url;private static String user;private static String password;static {try {Properties props new Properties();// 获得类加载器ClassLoader cl ConnectionUtils.class.getClassLoader();// 调用类加载器的方法去加载在类路径下的文件InputStream is cl.getResourceAsStream(com/tarena/tts/db.properties);// 将文件的内容读到props对象中props.load(is);driver props.getProperty(driver);url props.getProperty(url);user props.getProperty(user);password props.getProperty(password);} catch (Exception e) {throw new RuntimeException(e);}}}使用 File 对象
public static void getParam(String fileName) {Properties props new Properties();File file new File(fileName);try {// 读入 fileName 指定的文件FileInputStream fileInputStream new FileInputStream(file);// 加载解析输入流指定的文件props.load(fileInputStream);// 获取文件中键值对的值并赋值给全局变量url props.getProperty(url);dbUser props.getProperty(dbUser);dbPassword props.getProperty(dbPassword);driver props.getProperty(driver);} catch (IOException e) {e.printStackTrace();}}