最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JDBC連接Mysql的5種方式實(shí)例總結(jié)

 更新時(shí)間:2023年04月03日 14:16:31   作者:神筆碼農(nóng).  
JDBC是Java DataBase Connectivity技術(shù)的簡(jiǎn)稱,是一種可用于執(zhí)行 SQL語句的Java API,下面這篇文章主要給大家介紹了關(guān)于JDBC連接Mysql的5種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

測(cè)試環(huán)境說明

mysql數(shù)據(jù)庫:jdbc:mysql://localhost:3306/test

IDE:IDEA 2022

JDK:JDK8

mysql:mysql 5.7

JDBC:5.1.37

第一種方式

使用靜態(tài)加載驅(qū)動(dòng)方式,連接mysql

這種方式靈活性差,依賴性強(qiáng)

public void connection01() throws SQLException {
    // 注冊(cè)驅(qū)動(dòng)
    Driver driver = new Driver();
    // 創(chuàng)建Properties對(duì)象,用于保存mysql賬號(hào)和密碼鍵值對(duì)
    Properties properties = new Properties();
    properties.setProperty("user", "root");
    properties.setProperty("password", "123456");
    String url = "jdbc:mysql://localhost:3306/test";
    // 得到mysql的連接
    Connection connection = driver.connect(url, properties);
    // 得到可以與mysql語句進(jìn)行交互的對(duì)象
    Statement statement = connection.createStatement();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    statement.close();
    // 關(guān)閉與mysql的連接
    connection.close();

第二種方式

在第一種方式的基礎(chǔ)上使用反射動(dòng)態(tài)加載驅(qū)動(dòng),依賴性減小、靈活性提高

public void connection02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    // 使用反射動(dòng)態(tài)加載mysql驅(qū)動(dòng)件程序
    Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    Driver driver = (Driver) aClass.newInstance();
    // 創(chuàng)建Properties對(duì)象,用于保存mysql賬號(hào)和密碼鍵值對(duì)
    Properties properties = new Properties();
    properties.setProperty("user", "root");
    properties.setProperty("password", "123456");
    String url = "jdbc:mysql://localhost:3306/test";
    // 得到mysql的連接
    Connection connection = driver.connect(url, properties);
    // 得到可以與mysql語句進(jìn)行交互的對(duì)象
    Statement statement = connection.createStatement();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    statement.close();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    connection.close();
}

第三種方式

使用DriverManager統(tǒng)一進(jìn)行管理

public void connection03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
	// 使用反射動(dòng)態(tài)加載mysql驅(qū)動(dòng)件程序
    Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    Driver driver = (Driver) aClass.newInstance();
    String user = "root";
    String password = "123456";
    String url = "jdbc:mysql://localhost:3306/test";
    // 使用DriverManager加載Driver
    DriverManager.registerDriver(driver);
    // 得到mysql的連接
    Connection connection = DriverManager.getConnection(url, user, password);
    // 得到可以與mysql語句進(jìn)行交互的對(duì)象
    Statement statement = connection.createStatement();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    statement.close();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    connection.close();
}

第四種方式

其實(shí)Class.forName(“com.mysql.jdbc.Driver”)在底層已經(jīng)自動(dòng)加載好了Driver實(shí)例

所以Driver driver = (Driver) aClass.newInstance();這句話可以省略

這種方式也是開發(fā)中使用最多的一種方式

public void connection04() throws ClassNotFoundException, SQLException {
    // 使用反射動(dòng)態(tài)加載mysql驅(qū)動(dòng)件程序
    Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    String user = "root";
    String password = "123456";
    String url = "jdbc:mysql://localhost:3306/test";
    // 得到mysql的連接
    Connection connection = DriverManager.getConnection(url, user, password);
    // 得到可以與mysql語句進(jìn)行交互的對(duì)象
    Statement statement = connection.createStatement();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    statement.close();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    connection.close();
}

第五種方式

mysql5.16后可以不用Class.forName(“com.mysql.jdbc.Driver”);來加載驅(qū)動(dòng)了
從jdk1.5以后使用了jdbc4,不再需要顯示調(diào)用class.forName()注冊(cè)驅(qū)動(dòng)而是自動(dòng)調(diào)用驅(qū)動(dòng)jar包下META-INF\services\java.sql.Driver文本中的類名稱去注冊(cè)
建議還是寫上 CLass . forName(“com.mysql.jdbc.Driver”),更加明確,兼容性更好

這里同時(shí)使用properties配置文件實(shí)現(xiàn)動(dòng)態(tài)信息動(dòng)態(tài)讀取,靈活性得到提升

推薦使用這種方式

src/com/mysql/mysql.properties配置文件內(nèi)容如下

url=jdbc:mysql://localhost:3306/test
user=root
password=123456

連接mysql程序

public void connection05() throws SQLException, ClassNotFoundException, IOException {
    // 使用Properties讀取配置文件下的內(nèi)容
    Properties properties = new Properties();
    properties.load(new FileInputStream("src/com/mysql/mysql.properties"));
    String url = properties.getProperty("url");
    String user = properties.getProperty("user");
    String password = properties.getProperty("password");
    // 得到mysql的連接
    Connection connection = DriverManager.getConnection(url, user, password);
    // 得到可以與mysql語句進(jìn)行交互的對(duì)象
    Statement statement = connection.createStatement();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    statement.close();
    // 關(guān)閉與 mysql語句進(jìn)行交互的對(duì)象
    connection.close();
}

總結(jié)

到此這篇關(guān)于JDBC連接Mysql的5種方式的文章就介紹到這了,更多相關(guān)JDBC連接Mysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

磐石市| 图木舒克市| 蒙城县| 阿城市| 陇川县| 宁武县| 准格尔旗| 五常市| 淄博市| 宝清县| 延寿县| 威宁| 桦甸市| 嘉善县| 大名县| 吉隆县| 新乡县| 桐柏县| 台中县| 嵩明县| 涪陵区| 芦溪县| 武隆县| 南康市| 盐城市| 芜湖市| 宕昌县| 平遥县| 垫江县| 潍坊市| 鄯善县| 定边县| 福州市| 舟曲县| 博兴县| 文成县| 固原市| 新兴县| 顺平县| 临清市| 太保市|