使用反射获取类加载器来读取properties文件出现空指针异常,可以使用直接使用输入流读取properties文件,为什么教学视频中却可以使用反射?另外问下怎么在PC端提问,不是论坛发帖,单纯悬赏C币提问,我都是在手机上提,电脑上修改
@Test
public void getConnection() throws Exception {
/*
* 读取配置文件来获取数据库连接
*/
Properties properties = new Properties();
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
InputStream in = this.getClass().getClassLoader().getResourceAsStream("C:/Java/WprkSpace/JDBC/jdbc.properties");
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Driver driver = (Driver) Class.forName(driverClass).newInstance();
properties.put("user", user);
properties.put("password", password);
Connection connerction = driver.connect(jdbcUrl, properties);
System.out.println( connerction);
in.close();
}