一、 最古老的方法(通过 Driver 接口直接连接数据库)
1.首先创建一个 Driver 实现类的对象
Driver dirver = new com.mysql.jdbc.Driver();
2.准备连接数据库的基本信息:url、user、password
String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "123456");
3.调用 Driver 借口的 connect(url, info) 获取数据库连接
Connection connection = dirver.connect(url, info);
实例代码:
@Test
public void testDriver() throws SQLException {
// 1. 创建一个 Driver 实现类的对象
Driver dirver = new com.mysql.jdbc.Driver();
// 2. 准备链接数据库的基本信息:url, user, password
String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "123456");
// 3. 调用 Driver 借口的 connect(url, info) 获取数据库连接
Connection connection = dirver.connect(url, info);
System.out.println(connection);
}
二、编写一个通用的方法(通过Driver 接口实现)
1.定义数据库基本信息变量
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
2.创建一个properties文件,用来存放数据库连接基本信息
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_jdbc
user=root
password=123456
3.读取类路径下的jdbc.properties 文件
InputStream in =
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
4.从properties文件获取数据库基本信息
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
5.通过反射创建 Driver 对象
Driver driver = (Driver) Class.forName(driverClass).newInstance();
6.准备用户名和密码,并连接数据库
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
实例代码:
public Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 读取类路径下的jdbc.properties 文件
InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
// 通过反射创建 Driver 对象
Driver driver = (Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
// 通过 Driver 的 connect 方法连接数据库.
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
@Test
public void testGetConnection() throws Exception {
System.out.println(getConnection());
}
三、通过DriverManager 驱动管理类获取数据库连接
1.准备数据库连接的四个字符串
1).创建 properties 对象
Properties properties = new Properties();
2).获取jdbc.properties 对应的输入流
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
3).加载对应的输入流
properties.load(inputStream);
4).具体决定 user、password、url、driver四个字符串
String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
- 加载数据库驱动程序
Class.forName(driver);
- 通过 Drivermanager 的 getConnection() 方法获取数据库连接
return DriverManager.getConnection(jdbcUrl, user, password);
实例化代码
@Test
public void testGetConnection23() throws Exception {
System.out.println(getConnection2());
}
public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{
Properties properties = new Properties();
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(inputStream);
String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
Class.forName(driver);
return DriverManager.getConnection(jdbcUrl, user, password);
}