数据库连接报错,仔细检查代码没有发现问题
代码部分
配置文件代码:dbconfig.properties
driverClassName=com.sql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
mysqlUsername=root
mysqlPassword=1234
工具类:JdbcUtils.java
package cn.java.demo1;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
private static Properties prop = null;
static {
//加载配置文件
prop = new Properties();
InputStream in = JdbcUtils.class.getResourceAsStream("dbconfig.properties");
try {
prop.load(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
//加载驱动类
try {
Class.forName(prop.getProperty("driverClassName").trim());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws IOException, Exception,SQLException {
//得到Connection
return DriverManager.getConnection(prop.getProperty("url").trim(),prop.getProperty("mysqlUsername").trim(),prop.getProperty("mysqlPassword").trim());
}
}
测试类:Demo1.java
package cn.java.demo1;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
public class Demo1 {
public static void main(String[] args) throws IOException, SQLException, Exception {
Connection conn = JdbcUtils.getConnection();
System.out.println(conn);
}
}
请教一下各位大佬