詳解JDBC的概念及獲取數(shù)據(jù)庫連接的5種方式
一、JDBC概念
Java DataBase Connectivity(Java數(shù)據(jù)庫連接技術) 它是將Java與SQL結合且獨立于特定的數(shù)據(jù)庫系統(tǒng)的應用程序編程接口(API-它是一種可用于執(zhí)行SQL語句的JavaAPI,即由一組用Java語言編寫的類與接口所組成)
JDBC的設計目的:
它是一種規(guī)范,設計出來的主要目的是為了讓各個數(shù)據(jù)庫開發(fā)商為Java程序員提供標準的數(shù)據(jù)訪問類和接口,使得獨立于DBMS的Java應用程序的開發(fā)成為可能(數(shù)據(jù)庫改變,驅動程序跟著改變,但是應用程序不變) Java設計規(guī)范接口,各大數(shù)據(jù)庫產(chǎn)商遵守規(guī)范實現(xiàn),Java程序員不需要考慮實現(xiàn)細節(jié),只需要調用API即可
JDBC工作的基本流程:
一個基本的JDBC工作流程,分為以下幾步:
1、加載特定數(shù)據(jù)庫驅動器實現(xiàn)類,并注冊驅動器(Driver會注冊到DriverManager中)
2、根據(jù)特定的URL,返回可以接受此URL的數(shù)據(jù)庫驅動對象Driver
3、使用數(shù)據(jù)庫驅動 Driver 創(chuàng)建數(shù)據(jù)庫連接Connection會話
4、使用 Connection對象創(chuàng)建 用于操作sql的Statement對象
5、statement對象 .執(zhí)行 sql語句,返回結果ResultSet 對象
6、處理ResultSet中的結果
7、關閉連接,釋放資源
二、JDBC獲取數(shù)據(jù)庫連接的5種方式
方式一
public class ConnectionTest { @Test public void testConnection1() throws SQLException { //獲取Driver實現(xiàn)類對象 Driver driver = new com.mysql.jdbc.Driver(); //jdbc:mysql: 協(xié)議 //localhost ip地址 //3306 MySQL數(shù)據(jù)庫默認端口號 //test 需要連接的數(shù)據(jù)庫名稱 String url = "jdbc:mysql://localhost:3306/test"; Properties info = new Properties(); //設置連接的用戶名和名稱,user和password是固定的寫法 info.setProperty("user","root"); info.setProperty("passwor","root"); Connection connect = driver.connect(url, info); System.out.println(connect); } }
方式二
public class ConnectionTest { @Test public void testConnection2() throws Exception { //1.使用反射獲取Driver實現(xiàn)類對象 Class<?> clazz = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance(); //2.提供需要連接的數(shù)據(jù)庫 String url = "jdbc:mysql://localhost:3306/test"; //3.提供連接需要的用戶和密碼 Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","lxq"); //獲取鏈接 Connection connect = driver.connect(url, info); System.out.println(connect); } }
方式二是方式一的迭代,與方式一相比沒有出現(xiàn)第三方的API,有較好的移植性
方式三
public class ConnectionTest { @Test public void testConnection3() throws Exception { //1.使用反射獲取Driver實現(xiàn)類對象 Class<?> clazz = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance(); //2.提供需要連接的數(shù)據(jù)庫 String url = "jdbc:mysql://localhost:3306/test"; //3.提供連接需要的用戶和密碼 String user = "root"; String passwor = "root"; //4.注冊驅動 DriverManager.registerDriver(driver); //5.獲取連接 Connection connection = DriverManager.getConnection(url, user, passwor); System.out.println(connection); } }
使用DriverManager(驅動管理器)替換Driver,DriverManager是jdk提供的一個類,用來完成獲取連接的操作
方式四
public class ConnectionTest { @Test public void testConnection4() throws Exception { //1.提供連接所需信息 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String passwor = "root"; //2.使用反射加載驅動 Class.forName("com.mysql.jdbc.Driver"); //3.獲取連接 Connection connection = DriverManager.getConnection(url, user, passwor); System.out.println(connection); } }
與方式三相比,方式四省略了如下代碼:
Driver driver = (Driver) clazz.newInstance(); DriverManager.registerDriver(driver);
理由是,在com.mysql.jdbc.Driver類中有如下所示的靜態(tài)代碼塊,使用Class.forName()加載這個驅動時就會執(zhí)行這個靜態(tài)代碼塊,實現(xiàn)了注冊驅動
static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } }
方式五
public class ConnectionTest { @Test public void testConnection5() throws Exception { //1.獲取連接所需信息 InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties info = new Properties(); info.load(is); String url = info.getProperty("url"); String user = info.getProperty("user"); String password = info.getProperty("passwor"); String driverClass = info.getProperty("driverClass"); //2.使用反射加載驅動 Class.forName(driverClass); //5.獲取連接 Connection connection = DriverManager.getConnection(url, user, passwor); System.out.println(connection); } }
其中配置文件jdbc.properties在src目錄下:
url=jdbc:mysql://localhost:3306/test user=root passwor=root; driverClass=com.mysql.jdbc.Driver
使用配置文件的好處:
1、實現(xiàn)代碼和數(shù)據(jù)的分離,如果需要修改配置信息,直接在配置文件中進行修改,不需要深入代碼
2、如果修改配置信息,省去了重新編譯的過程=
到此這篇關于詳解JDBC的概念及獲取數(shù)據(jù)庫連接的5種方式的文章就介紹到這了,更多相關JDBC數(shù)據(jù)庫連接內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IntelliJ IDEA使用快捷鍵重命名項目、變量、文件等方法總結
今天小編就為大家分享一篇關于IntelliJ IDEA使用快捷鍵重命名項目、變量、文件等方法總結,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10springboot結合vue實現(xiàn)增刪改查及分頁查詢
本文主要介紹了springboot結合vue實現(xiàn)增刪改查及分頁查詢,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09