数据库的连接*
第一步:首先配置文件中的信息
第二步:加载驱动
第三步:获取连接`
jdbc.properies的配置
username=root
password=root
url=jdbc:mysql://localhost:3306/book?rewriteBatchedStatements=true
driverClass=com.mysql.jdbc.Driver
public class Connection {
@Test
public void getConnection() throws Exception{
//读取配置文件中的信息
InputStream inputStream =ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
// InputStream inputStream1 =ClassLoader.getSystemResourceAsStream("jdbc.properties");
Properties properties =new Properties();
properties.load(inputStream);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverClass = properties.getProperty("driverClass");
//加载驱动
Class.forName(driverClass);
//获取连接
java.sql.Connection connection = DriverManager.getConnection(url, username, password);
System.out.println(connection);
}
}