mySQL配置文件报静态代码块执行异常,连接数据库失败,mySQL已开

weixin_42504422 weixin_42504422 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

public class JDBCUtilsConfig {
private static Connection con ;
private static String driverClass;
private static String url;
private static String username;
private static String password;
/*public static void main(String[] args) throws Exception{
readConfig();
System.out.println(driverClass);
System.out.println(url);
System.out.println(username);
System.out.println(password);

}*/
//打印的几项都没有任何问题
/*static {
try {
readConfig();
Class.forName(driverClass);
con = DriverManager.getConnection(url, username, password);
}catch(Exception ex) {
throw new RuntimeException("连接数据库失败");
}
}*/

private static void readConfig() throws Exception{
InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
Properties pro = new Properties();
pro.load(in);
driverClass=pro.getProperty("driverClass");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
}

public static Connection getConnection() {
return con;
}
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
weixin_42504422
Java 51,407 成员 86,130 内容