1. 程式人生 > 資料庫 >JDBC中,連線資料庫的五種方式

JDBC中,連線資料庫的五種方式

今天寫一篇關於jdbc連線資料庫的五個方式,從第一種到第五種,程式碼逐漸簡化,更具有健壯性!

url的地址如下如圖:

 

 

第一種

public void TestConnection1() throws SQLException {
        //獲取Driver實現類的物件
        Driver driver = new com.mysql.jdbc.Driver();

        String url = "jdbc:mysql://localhost:3306";
        //將使用者名稱和密碼封裝到Properties
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        Connection connect = driver.connect(url, properties);

        System.out.println(connect);
    }

 

第二種:對方式一的迭代,在如下的程式中不出現第三方的api,使得程式具有更好的可移植性

public void TestConnection2() throws Exception {
        //1,獲取Driver實現類的物件,使用反射
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2,提供要連線的資料庫
        String url = "jdbc:mysql://localhost:3306/jdbc";

        //3,提供連線需要的使用者名稱和密碼
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");

        //4,獲取連線
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

 

第三種:使用DriverManager

public void TestConnection3() throws Exception {
        //1,獲取Driver實現類的物件
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2,提供另外三個連線的基本資訊
        String url = "jdbc:mysql://localhost:3306/jdbc";
        String user = "root";
        String password = "123456";

        //3,註冊驅動
        DriverManager.registerDriver(driver);

        //4,獲取連線
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

  

第四種:可以只是載入驅動,不用顯示的註冊驅動了

public void TestConnection4() throws Exception {
        //1,提供三個基本資訊
        String url = "jdbc:mysql://localhost:3306/jdbc";
        String user = "root";
        String password = "123456";

        //2,載入Driver
        Class.forName("com.mysql.jdbc.Driver");

        //3,獲取連線
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }

  

第五種方式:也是最終的方式,將資料庫所需要的四個基本資訊宣告在配置檔案種,通過讀取配置檔案的方式,獲取連線

這種方式的話需要先在src目錄下建立一個配置檔案,一般命名為 jdbc.properties。然後寫下這四個基本資訊

 

 然後程式碼如下,第一步就是讀取配置檔案,第二步的話載入驅動,第三步獲取連線

//1,讀取配置檔案中的四個基本資訊
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");

        Properties properties = new Properties();
        properties.load(is);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        //2,載入驅動
        Class.forName(driver);

        //3,獲取連線
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

  

這也是我自己看視訊學習到的一部分,一共介紹了這五種,最重要的就是第五種,也是之後要用到的一種方法,所以有小夥伴要參考的話就可以直接參考最後一種,如果有什麼問題可以留言,我會盡力解決,我寫的東西有錯誤也可以提出來,歡迎大家討論