Java連接Mysql數據庫的五種方法


說明: 本文通過maven構建,連接的數據庫是mysql 數據庫

pom.xml

 <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>
pom.xml

數據庫連接方式一

 public static void Connection1() throws SQLException {
        Driver drive=new com.mysql.jdbc.Driver();
        String  url="jdbc:mysql://localhost:3306/test";
        Properties info=new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");
        Connection connect = drive.connect(url, info);
        System.out.println(connect);
    }
獲取數據庫連接的方式一

數據庫連接方式二

 public static void Connection2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        // 1.獲取Driver實現類對象:使用反射
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        // 2.提供要連接的數據庫
        String url = "jdbc:mysql://localhost:3306/test";
        // 3.提供連接需要的用戶名和密碼
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "root");
        // 4.獲取連接
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }
數據庫連接方式二

 

數據庫連接方式三

 // 1.獲取Driver實現類的對象
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        // 2.提供另外三個連接的基本信息:
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "root";
        // 注冊驅動
        DriverManager.registerDriver(driver);
        // 獲取連接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
數據庫連接方式三

 

數據庫連接方式四

String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "root";

        // 2.加載Driver
        Class.forName("com.mysql.jdbc.Driver");
        // 3.獲取連接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
數據庫連接方式四

 

數據庫連接方式五 非常推薦使用

好處:

  • 1.實現了數據與代碼的分離。實現了解耦

  • 2.如果需要修改配置文件信息,可以避免程序重新打包。

user=root
password=root
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
driverClass=com.mysql.jdbc.Driver
配置文件jdbc.properties
        InputStream is = connDate.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        //2.加載驅動
        Class.forName(driverClass);
        //3.獲取連接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
數據庫連接方式五

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM