一、 最古老的方法(通過 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 @Test 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 { 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 @Test 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
