下载对应版的数据库驱动
#配置文件
url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
user=root
password=123456789
driver=com.mysql.cj.jdbc.Driver
package test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionTest {
public static void main(String[] args) throws Exception {
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("E:\\IdeaProjects\\Demo\\src\\test\\database.properties"));
// 读取配置文件
Properties properties = new Properties();
properties.load(inputStream);
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driverStr = properties.getProperty("driver");
Class.forName(driverStr); // 连接数据库
// 返回Connection对象
Connection driverManager = DriverManager.getConnection(url, user, password);
}
}