一、通過Driver接口連接數據庫
Driver是JAVA中的一個接口,它的實現是由數據庫廠商去實現的,能夠通過它去獲取數據庫的連接,想要使用Driver,需要通過如下步驟:
①從Mysql官網下載到驅動後解壓
②在當前項目中新建一個文件夾
③將解壓後的驅動文件夾中的jar結尾的文件添加到步驟②中新建的文件夾的目錄下
④右鍵單擊jar文件,選擇bulid path,然後add to bulidpath
導入了之後我們就可以根據Driver的實體對象去獲取數據庫的連接,實現方法如下:
①創建一個Driver實現類的對象
Driver driver = new com.mysql.jdbc.Driver();
②創建連接數據庫的基本信息String類型的name,Properties類型的info
String url = "jdbc:mysql://localhost:3306/sid" ;
Properties info = new Properties();
url的書寫方式如上面所示,上面是連接本地Mysql的方式,鏈接最後的sid爲要連接的數據庫名
③調用Driver接口的connect(url,info)方法獲取數據庫連接
Connection connection = driver .connect(url, info);
System. out.println(connection );
但是這個方法有個弊端,就是不能解耦,當在連接其他數據庫的時候就要重寫這個方法,所以我們採用將連接數據庫所需的信息存放在文件中,當需要連接不同的數據庫廠商時可以修改配置文件從而達到解耦,具體操作如下:
1、在當前項目中創建一個文件名爲"jdbc.properties",並寫入如下內容並保存
driver=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://localhost:3306/test
user=root
password=090936//這裏的密碼是安裝mysql等數據庫時所設置的密碼
2、然後在相應的方法中讀取該文件,按照以下方式執行
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) Class.forName(driverClass).newInstance(); //反射創建Driver對象
Properties info = new Properties();
info.put( "user", user );
info.put( "password", password );
Connection connection = driver .connect(jdbcurl, info);
return connection ;
}
像上面這樣,就可以做到對任意數據庫廠商的連接了,而我們只需要更改配置文件的信息即可
二、通過DriverManager獲取數據庫連接
DriverManager是驅動的管理類,用它來連接數據庫比Driver更爲方便,只需要利用反射加載好數據庫的驅動程序然後再調用其中的getConnection()方法即可完成數據庫的連接,因爲可以通過改變傳入getConnection()方法的參數來返回不同的數據庫連接,所以它可以管理不同的驅動程序,下面就是通過DriverManager來獲取數據庫連接的步驟:
1、在當前項目中創建一個文件名爲"jdbc.properties",並寫入如下內容並保存
driver=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://localhost:3306/test //注意這裏連接的是mysql數據庫
user=root
password=090936//這裏的密碼是安裝mysql等數據庫時所設置的密碼
2、進行連接數據庫的一系列操作,具體過程如下
public Connection getConnection1() throws Exception{
// 1、創建連接數據庫的基本信息
String driverClass = null;
String url = null;
String user = null;
String password = null;
// 2、創建Properties對象並獲取jdbc.Properties對應的輸入流
Properties properties = new Properties();
InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
// 3、加載對應的輸入流
properties.load(in);
// 獲取輸入流中的信息從而決定連接數據庫的基本信息的內容
driverClass = properties.getProperty("driver");
url = properties.getProperty("jdbcurl");
user = properties.getProperty("user");
password = properties.getProperty("password");
// 5、利用反射加載數據庫驅動程序
Class.forName(driverClass);
// 6、通過DriverManager中的getConnection()方法獲取數據庫連接
return DriverManager.getConnection(url, user, password);
}
3、進行連接的測試
在完成了前兩個步驟之後,在主函數中進行測試,測試內容如下
public static void main(String[] args) throws Exception {
TestDriverManager e = new TestDriverManager();
System.out.println(e.getConnection1());
}
若輸出語句爲 com.mysql.jdbc.JDBC4Connection@xxxxxx 則數據庫的連接完成了