通过properties读取数据

来源:互联网 发布:听旋律识音谱的软件 编辑:程序博客网 时间:2024/06/05 21:07

首先将数据以key=value形式编写入jdbc.properties文件中



然后定义一个实体类PropertiesUtil,定义属性private final String url= DataUtil.getProperty("url"); 并给出getter方法

其中定义DataUtil类:

public class DataUtil {    private static Properties prop;    static{        loadProps();    }    private synchronized static void loadProps() {        prop = new Properties();        InputStream in = null;        try {            in = DataUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");            prop.load(in);        } catch (FileNotFoundException e) {            e.printStackTrace();            logger.error("文件未找到");        } catch (IOException e) {            e.printStackTrace();            logger.error("IO异常");        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException e) {                    e.printStackTrace();                    logger.error("InputStream流关闭异常");                }            }        }    }    public static String getProperty(String key){        if(null == prop) {            loadProps();        }        return prop.getProperty(key);    }}

new PropertiesUtil().getUrl(); 既是

原创粉丝点击