(1)配置文件放在项目的根目录下
Properties properties = new Properties(); InputStream is = new FileInputStream("config.properties"); // 导入输入流 properties.load(is); // 读取 driverClass = properties.getProperty("driverClass"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password");
(2)配置文件放在src目录下
Properties properties = new Properties(); InputStream is = JDBCutils.class.getClassLoader().getResourceAsStream("config.properties"); // 导入输入流 properties.load(is); // 读取 driverClass = properties.getProperty("driverClass"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password");