一、 最古老的方法(通过 Driver 接口直接连接数据库)
- 首先创建一个 Driver 实现类的对象
Driver dirver = new com.mysql.jdbc.Driver();
- 准备连接数据库的基本信息:url、user、password
1 String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
2 Properties info = new Properties();
3 info.put("user", "root");
4 info.put("password", "123456"); - 调用 Driver 借口的 connect(url, info) 获取数据库连接
1 Connection connection = dirver.connect(url, info);
实例代码:
1 @TestView Code
2 public void testDriver() throws SQLException {
3 // 1. 创建一个 Driver 实现类的对象
4 Driver dirver = new com.mysql.jdbc.Driver();
5
6 // 2. 准备链接数据库的基本信息:url, user, password
7 String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
8 Properties info = new Properties();
9 info.put("user", "root");
10 info.put("password", "123456");
11
12 // 3. 调用 Driver 借口的 connect(url, info) 获取数据库连接
13 Connection connection = dirver.connect(url, info);
14 System.out.println(connection);
15 }
二、编写一个通用的方法(通过Driver 接口实现)
- 定义数据库基本信息变量
1 String driverClass = null;
2 String jdbcUrl = null;
3 String user = null;
4 String password = null; - 创建一个properties文件,用来存放数据库连接基本信息
1 driver=com.mysql.jdbc.Driver
2 jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_jdbc
3 user=root
4 password=123456 - 读取类路径下的jdbc.properties 文件
1 InputStream in =
2 getClass().getClassLoader().getResourceAsStream("jdbc.properties");
3 Properties properties = new Properties();
4 properties.load(in); - 从properties文件获取数据库基本信息
1 driverClass = properties.getProperty("driver");
2 jdbcUrl = properties.getProperty("jdbcUrl");
3 user = properties.getProperty("user");
4 password = properties.getProperty("password"); - 通过反射创建 Driver 对象
1 Driver driver = (Driver) Class.forName(driverClass).newInstance();
- 准备用户名和密码,并连接数据库
1 Properties info = new Properties();
2 info.put("user", user);
3 info.put("password", password);
4 Connection connection = driver.connect(jdbcUrl, info);
实例代码:
1 public Connection getConnection() throws Exception {View Code
2 String driverClass = null;
3 String jdbcUrl = null;
4 String user = null;
5 String password = null;
6
7 // 读取类路径下的jdbc.properties 文件
8 InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
9 Properties properties = new Properties();
10 properties.load(in);
11
12 driverClass = properties.getProperty("driver");
13 jdbcUrl = properties.getProperty("jdbcUrl");
14 user = properties.getProperty("user");
15 password = properties.getProperty("password");
16
17 // 通过反射创建 Driver 对象
18 Driver driver = (Driver) Class.forName(driverClass).newInstance();
19
20 Properties info = new Properties();
21 info.put("user", user);
22 info.put("password", password);
23
24 // 通过 Driver 的 connect 方法连接数据库.
25 Connection connection = driver.connect(jdbcUrl, info);
26
27 return connection;
28 }
29
30 @Test
31 public void testGetConnection() throws Exception {
32 System.out.println(getConnection());
33 }
三、通过DriverManager 驱动管理类获取数据库连接
- 准备数据库连接的四个字符串
1).创建 properties 对象
1 Properties properties = new Properties();
2).获取jdbc.properties 对应的输入流
1 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
3).加载对应的输入流
1 properties.load(inputStream);
4).具体决定 user、password、url、driver四个字符串
1 String driver = properties.getProperty("driver");
2 String jdbcUrl = properties.getProperty("jdbcUrl");
3 String user = properties.getProperty("user");
4 String password = properties.getProperty("password");
2. 加载数据库驱动程序
1 Class.forName(driver);
3. 通过 Drivermanager 的 getConnection() 方法获取数据库连接
1 return DriverManager.getConnection(jdbcUrl, user, password);
实例代码:
1 @TestView Code
2 public void testGetConnection23() throws Exception {
3 System.out.println(getConnection2());
4 }
5 public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{
6
7 Properties properties = new Properties();
8
9 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
10
11 properties.load(inputStream);
12
13 String driver = properties.getProperty("driver");
14 String jdbcUrl = properties.getProperty("jdbcUrl");
15 String user = properties.getProperty("user");
16 String password = properties.getProperty("password");
17
18 Class.forName(driver);
19
20 return DriverManager.getConnection(jdbcUrl, user, password);
21 }
本文只是作者的笔记,只供参考,有错误欢迎指出!2017-10-28 11:14:03