今天在写jdbc工具类的时候,出现了这个问题。
Could not initialize class 包名.JDBCUtils
经过查询相关资料,才知道了问题出在这块代码
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
// 此处出现中文路径乱码
String path = res.getPath();
导致后面的代码接收路径的时候直接报错。
将此片代码改为这样:
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
// String path = res.getPath();
// 此处因为路径包含中文,故使用URLDecoder.decode(str,"UTF-8")方法解决。
String path = URLDecoder.decode(res.getPath(),"UTF-8");
再次尝试,发现问题已经解决。