Java cannot connect to the database with the most complete configuration file

One: Jar package corresponding to data version 8.0
Insert picture description here
Two: Connected driver
Insert picture description here
Three: Configuration file
url=jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8&&rewriteBatchedStatements=true
username=root
password=123456
driverClassName=com .mysql.cj.jdbc.Driver

四:连接的代码
static {
// 第一步: 先读取配置文件
try {
Properties properties=new Properties();
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream(“jdbc.properties”);
properties.load(resourceAsStream);
DataSource dataSource1 = DruidDataSourceFactory.createDataSource(properties);
try (Connection connection = dataSource1.getConnection()) {
System.out.println(connection);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}

Five: Matters needing attention
① Start the database service
② Check whether the storage location of the configuration file is correct
③ Whether the url connection is written correctly

Guess you like

Origin blog.csdn.net/weixin_46351306/article/details/115115273