如下共有四個鏈接數據庫的方法:java
1.testDriver():該方法使用驅動對象(driver)創建鏈接,可重用性差,耦合性高,但代碼簡單,易於理解。mysql
2.getConnection():該方法在上一個方法的基礎上進行了修改,不過仍是經過驅動對象創建鏈接(driver),
*好比:把鏈接數據庫的url、帳號、密碼寫道配置文件中,程序再讀取配置文件,經過反射加載驅動,並建立驅動對象,程序員
3.testDriverManager():該方法使用驅動管理類的getConnection()方法創建鏈接,在上一個方法的基礎上進行了修改,
*修改了加載驅動的方法,由於使用了驅動管理類,因此不須要建立驅動對象,減小了代碼量。web
4.getDriverManager():該方法是對以上幾個方法一個總結,相對來講少代碼,高重用性,低耦合 sql
/** * 1.簡單鏈接數據庫 * 在使用的過程當中並不建議直接訪問該接口,在此只是練習演示。 * Driver是一個接口,數據庫廠商必須提供實現的接口,能從中獲取數據庫鏈接, * 步驟: * 1.建立數據庫驅動對象 * 2.準備數據庫鏈接的url、user、password * 3.數據庫驅動對象調用connect(url , info)方法創建鏈接 */
@Test
public void testDriver() throws Exception{
//1.驅動
Driver driver = new com.mysql.jdbc.Driver();
//2.準備url,帳號、密碼
String url = "jdbc:mysql://127.0.0.1:3306/test";//test數據庫首先要在建立好以後才能夠鏈接
Properties info = new Properties();
info.put("user", "root");
info.put("password", "");
//3.創建連接
Connection connection = driver.connect(url , info);
System.out.println(connection);
}
/** * 2.對以上方法進行修改優化,在不改變源程序的狀況下,能夠得到任何數據庫鏈接 * 解決方案:把數據庫驅動Driver實現類是全類名、url、user、passworld放到一個配置文件中, *經過修改配置文件的方式實現的具體數據庫的解耦。 *步驟: *1.準備url、帳號、密碼、驅動的全類名的字段 *2.經過反射加載配置文件 *3.經過getProperty()獲取配置文件中的信息 *4.經過反射建立驅動程序對象(連接數據庫須要用到該對象,因此要建立) *5.驅動對象調用connect(jdbcUrl, info)創建鏈接 */
public Connection getConnection() throws Exception{
//1.準備字段
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
//2.經過反射,讀取類路徑下的jdbc.properties文件,獲取配置信息。
InputStream is = getClass().getClassLoader().
getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
//3.經過getProperty()方法,獲取到每一個字段的值,並賦給變量
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
//4.經過反射來進行建立驅動對象,這樣就能夠實現解耦。
//4_1.建立驅動程序對象
Driver driver = (Driver)Class.forName(driverClass).newInstance();
//4_2.帳號密碼
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
//5.調用connect方法創建鏈接
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
/** * 測試以上方法是否正確 * @throws Exception */
@Test
public void testGetConnection() throws Exception{
System.out.println(getConnection());
}
/** * 3.對以上方法再次進行修改優化。 * DriverManager是驅動的管理類,聯繫使用 * 步驟: * 1.準備驅動的全類名、url、user、password字段 * 2.經過反射讀取配置文件信息 * 3.經過反射加載數據庫驅動程序(由於使用DriverManager,因此連接數據庫不須要驅動程序對象,因此不須要建立) * 4.使用DriverManager類的getConnection(jdbcUrl, user, password)方法創建鏈接 */
@Test
public void testDriverManager() throws Exception{
//1.準備字段
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
//2.讀取類路徑下的jdbc.properties文件,獲取配置信息
InputStream is = getClass().getClassLoader().
getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
//3.經過反射來進行建立驅動對象,這樣就能夠實現解耦。
//3_1.加載數據庫驅動程序(註冊一個驅動程序)
Class.forName(driverClass);
//4.調用connect方法創建鏈接
Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
System.out.println(connection);
}
/** * 4.總結。 * 步驟: * 1.準備連接數據庫的4個字符串 * 1_1.建立Properties對象 * 1_2.獲取jabc.properties對應的輸入流 * 1_3.加載1_2對應的輸入流 * 1_4.獲取具體數據(Driver、url、user、password) * 2.加載數據庫驅動程序(對應的Driver實現類中有註冊驅動的代碼塊) * 3.經過DriverManager的getConnection()方法獲取數據庫連接 * */
public Connection getDriverManager() throws Exception{
//1_1.建立Properties對象
Properties properties = new Properties();
//1_2.獲取jdbc.properties輸入流
InputStream is = getClass().getClassLoader().
getResourceAsStream("jdbc.properties");
//1_3.加載對應發輸入流
properties.load(is);
//1_4.獲取具體信息
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String jdbcUrl = properties.getProperty("jdbcUrl");
String driver = properties.getProperty("driver");
//2.加載數據庫驅動程序
Class.forName(driver);
//3.經過DriverManager的getConnection()方法獲取數據庫連接,並返回
return DriverManager.getConnection(jdbcUrl, user, password);
}
/** * 測試以上方法 */
@Test
public void testGetConnection1(){
try {
System.out.println(getDriverManager());
} catch (Exception e) {
e.printStackTrace();
}
}
jdbc.properties配置文件數據庫
#鏈接mysql驅動,其中?characterEncoding=utf-8是爲防止亂碼
driver = com.mysql.jdbc.Driver
jdbcUrl = jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8
user = root
password =
#鏈接sqlserver2008驅動
#driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbcUrl = jdbc:sqlserver://127.0.0.1:1433;DatabaseName = SIMS
#user = sa
#password = admin