学习写代码呀

导航

两种读取数据库配置文件的方式

1、ResourceBundle

ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); //读取配置文件 配置文件在类的同一目录下,不同时需要加入路径 jdbc.properties
         url = bundle.getString("url");
         user = bundle.getString("user");
         password = bundle.getString("password");
         driverclass = bundle.getString("driverclass");

2、Properties

Properties properties = new Properties();
        try {
            properties.load(this.getClass().getResourceAsStream("/jdbc.properties"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String driver = properties.getProperty("driver");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");

posted on 2019-09-16 14:12  学习写代码呀  阅读(631)  评论(0编辑  收藏  举报