将properties文件放入src下、通过java代码读取
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class test3 {
public static void main(String[] args) throws IOException {
InputStream is = test3.class.getClassLoader().getResourceAsStream(“jdbc.properties”);
Properties p = new Properties();
p.load(is);
String username = p.getProperty("jdbc.username");
String password = p.getProperty("jdbc.password");
String localhost = p.getProperty("jdbc.host");
String port = p.getProperty("jdbc.port");
System.out.println(username);
System.out.println(password);
System.out.println(localhost);
System.out.println(port);
}
}
properties文件如下:
jdbc.username=root
jdbc.password=root
jdbc.host=localhost
jdbc.port=3306