JDBC連接Mysql的5種方式實例總結
測試環(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)加載驅動方式,連接mysql
這種方式靈活性差,依賴性強
public void connection01() throws SQLException {
// 注冊驅動
Driver driver = new Driver();
// 創(chuàng)建Properties對象,用于保存mysql賬號和密碼鍵值對
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語句進行交互的對象
Statement statement = connection.createStatement();
// 關閉與 mysql語句進行交互的對象
statement.close();
// 關閉與mysql的連接
connection.close();
第二種方式
在第一種方式的基礎上使用反射動態(tài)加載驅動,依賴性減小、靈活性提高
public void connection02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
// 使用反射動態(tài)加載mysql驅動件程序
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
// 創(chuàng)建Properties對象,用于保存mysql賬號和密碼鍵值對
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語句進行交互的對象
Statement statement = connection.createStatement();
// 關閉與 mysql語句進行交互的對象
statement.close();
// 關閉與 mysql語句進行交互的對象
connection.close();
}
第三種方式
使用DriverManager統(tǒng)一進行管理
public void connection03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
// 使用反射動態(tài)加載mysql驅動件程序
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語句進行交互的對象
Statement statement = connection.createStatement();
// 關閉與 mysql語句進行交互的對象
statement.close();
// 關閉與 mysql語句進行交互的對象
connection.close();
}
第四種方式
其實Class.forName(“com.mysql.jdbc.Driver”)在底層已經(jīng)自動加載好了Driver實例
所以Driver driver = (Driver) aClass.newInstance();這句話可以省略
這種方式也是開發(fā)中使用最多的一種方式
public void connection04() throws ClassNotFoundException, SQLException {
// 使用反射動態(tài)加載mysql驅動件程序
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語句進行交互的對象
Statement statement = connection.createStatement();
// 關閉與 mysql語句進行交互的對象
statement.close();
// 關閉與 mysql語句進行交互的對象
connection.close();
}
第五種方式
mysql5.16后可以不用Class.forName(“com.mysql.jdbc.Driver”);來加載驅動了
從jdk1.5以后使用了jdbc4,不再需要顯示調(diào)用class.forName()注冊驅動而是自動調(diào)用驅動jar包下META-INF\services\java.sql.Driver文本中的類名稱去注冊
建議還是寫上 CLass . forName(“com.mysql.jdbc.Driver”),更加明確,兼容性更好
這里同時使用properties配置文件實現(xiàn)動態(tài)信息動態(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語句進行交互的對象
Statement statement = connection.createStatement();
// 關閉與 mysql語句進行交互的對象
statement.close();
// 關閉與 mysql語句進行交互的對象
connection.close();
}
總結
到此這篇關于JDBC連接Mysql的5種方式的文章就介紹到這了,更多相關JDBC連接Mysql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
idea perttier的使用和縮進改為4不成功問題及解決
這篇文章主要介紹了idea perttier的使用和縮進改為4不成功問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
eclipse實現(xiàn)可認證的DH密鑰交換協(xié)議
這篇文章主要介紹了eclipse實現(xiàn)可認證的DH密鑰交換協(xié)議,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
Spring Boot集成Spring Cloud Eureka進行服務治理的方法
本文通過詳細的步驟和代碼示例,介紹了如何在Spring Boot中集成Spring Cloud Eureka進行服務治理,通過這種方式,可以有效地管理和維護微服務架構中的服務,感興趣的朋友跟隨小編一起看看吧2024-11-11
Java為何需要平衡方法調(diào)用與內(nèi)聯(lián)
這篇文章主要介紹了Java為何需要平衡方法調(diào)用與內(nèi)聯(lián),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-01-01

