mysql drivermanager_MYSQL 之 JDBC(二): 数据库连接(二)通过DriverManager获取数据库连接...
通過DriverManager獲取數(shù)據(jù)庫連接
修改一下配置文件
driver=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/testjdbc?serverTimezone=GMT%2B8
user=root
password=123456
代碼(我覺得廢話有點多,同一個知識點翻來覆去的講,并且有的疑點還沒解決)
比如說:利用Driver和DriverManager都能用不同的數(shù)據(jù)庫,為什么DriverManager更好
package com.litian.jdbc;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* @author: Li Tian
* @contact: litian_cup@163.com
* @software: IntelliJ IDEA
* @file: JDBCTest.java
* @time: 2019/12/15 18:56
* @desc: JDBC試驗,Driver是一個接口:數(shù)據(jù)庫廠商必須提供實現(xiàn)的接口,能從其中獲取數(shù)據(jù)庫連接。
*/
public class JDBCTest {
public Connection getConnection2() throws Exception {
// 1. 準備連接數(shù)據(jù)庫的4個字符串。
// 1.1 創(chuàng)建Properties對象
Properties properties = new Properties();
// 1.2 獲取jdbc.properties對應(yīng)的輸入流
InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
// 1.3 加載1.2對應(yīng)的輸入流
properties.load(in);
// 1.4 具體決定user,password等4個字符串。
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String jdbcUrl = properties.getProperty("jdbcUrl");
String driver = properties.getProperty("driver");
// 2. 加載數(shù)據(jù)庫驅(qū)動程序
Class.forName(driver);
// 3. 通過DriverManager的getConnection()方法獲取數(shù)據(jù)庫連接。
return DriverManager.getConnection(jdbcUrl, user, password);
}
/**
* DriverManager是驅(qū)動的管理類
* 1. 可以通過重載的getConnection()方法獲取數(shù)據(jù)庫連接。較為方便
* 2. 可以同時管理多個驅(qū)動程序:若注冊了多個數(shù)據(jù)庫連接,則調(diào)動getConnection()方法時
* 傳入的參數(shù)不同,則返回不同的數(shù)據(jù)庫連接
*/
public void testDriverManager() throws Exception {
// 1. 準備連接數(shù)據(jù)庫的4個字符串
// 驅(qū)動的全類名
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 讀取類路徑下的jdbc.propertites 文件
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");
// 2. 加載數(shù)據(jù)庫驅(qū)動程序(對應(yīng)的Driver實現(xiàn)類中有注冊驅(qū)動的靜態(tài)代碼塊程序)
// 下面的注冊程序已經(jīng)寫好了,不需要自己寫
// DriverManager.registerDriver((Driver) Class.forName(driverClass).newInstance());
Class.forName(driverClass);
// 3. 通過DriverManager的getConnection()方法獲取數(shù)據(jù)庫連接
Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
System.out.println(connection);
}
public void test1() throws SQLException {
// 1. 創(chuàng)建一個Driver實現(xiàn)類的對象
Driver driver = new com.mysql.jdbc.Driver();
// 2. 準備連接數(shù)據(jù)庫的基本信息:url,user,password
String url = "jdbc:mysql://localhost:3306/girls";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "tian19951103");
// 3. 調(diào)用Driver接口的connect(url, info)獲取數(shù)據(jù)庫連接
Connection connection = driver.connect(url, info);
System.out.println(connection);
}
// 編寫一個通用的方法,在不修改源程序的情況下,可以獲取任何數(shù)據(jù)庫的連接
public Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 讀取類路徑下的jdbc.propertites 文件
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) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
public void testGetConnection() throws Exception {
System.out.println(getConnection());
}
public static void main(String[] args) throws Exception {
// new JDBCTest().testGetConnection();
// new JDBCTest().testDriverManager();
Connection conn = new JDBCTest().getConnection2();
System.out.println(conn);
}
}
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的mysql drivermanager_MYSQL 之 JDBC(二): 数据库连接(二)通过DriverManager获取数据库连接...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: std map多线程_SEBR:多线程内
- 下一篇: linux mysql tmp_linu