问题遇到的现象和发生背景
代码总是报错:Exception in thread "main" java.lang.ExceptionInInitializerError和java.lang.NullPointerException,好像是静态初始化块中出现了异常的时候出现。请问怎么解决?
用代码块功能插入代码,请勿粘贴截图
package itcast.util;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class jdbcUtils {
private static String url;
private static String username;
private static String password;
private static String driver;
//获去资源
static {
try {
//创建Properties集合类
Properties pro = new Properties();
ClassLoader classLoader = jdbcUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
//加载文件
pro.load(new FileReader(path));
//获去数据赋值
url= pro.getProperty("url");
username=pro.getProperty("user");
password=pro.getProperty("password");
driver=pro.getProperty("driver");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接对象
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
//释放资源
public static void close(ResultSet rs, Statement stmt, Connection conn){
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
*/