JDBC連接Mysql的5種方式實(shí)例總結(jié)
測試環(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 { // 注冊驅(qū)動(dòng) 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語句進(jìn)行交互的對象 Statement statement = connection.createStatement(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 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對象,用于保存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語句進(jìn)行交互的對象 Statement statement = connection.createStatement(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 statement.close(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 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)行交互的對象 Statement statement = connection.createStatement(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 statement.close(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 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)行交互的對象 Statement statement = connection.createStatement(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 statement.close(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 connection.close(); }
第五種方式
mysql5.16后可以不用Class.forName(“com.mysql.jdbc.Driver”);來加載驅(qū)動(dòng)了
從jdk1.5以后使用了jdbc4,不再需要顯示調(diào)用class.forName()注冊驅(qū)動(dòng)而是自動(dòng)調(diào)用驅(qū)動(dòng)jar包下META-INF\services\java.sql.Driver文本中的類名稱去注冊
建議還是寫上 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)行交互的對象 Statement statement = connection.createStatement(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 statement.close(); // 關(guān)閉與 mysql語句進(jìn)行交互的對象 connection.close(); }
總結(jié)
到此這篇關(guān)于JDBC連接Mysql的5種方式的文章就介紹到這了,更多相關(guān)JDBC連接Mysql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 后臺(tái)將base64字符串保存為圖片的方法
本篇文章主要介紹了java 后臺(tái)將base64字符串保存為圖片的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情
這篇文章主要介紹了利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情,根據(jù)概率將獎(jiǎng)品劃分區(qū)間,每個(gè)區(qū)間代表一個(gè)獎(jiǎng)品,然后抽取???隨機(jī)數(shù)??,反查落在那個(gè)區(qū)間上,即為所抽取的獎(jiǎng)品,需要的朋友可以參考一下2022-07-07Spring AOP訪問目標(biāo)方法的參數(shù)操作示例
這篇文章主要介紹了Spring AOP訪問目標(biāo)方法的參數(shù)操作,結(jié)合實(shí)例形式詳細(xì)分析了spring面向切面AOP訪問目標(biāo)方法的參數(shù)相關(guān)實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01如何使用Resttemplate和Ribbon調(diào)用Eureka實(shí)現(xiàn)負(fù)載均衡
這篇文章主要介紹了如何使用Resttemplate和Ribbon調(diào)用Eureka實(shí)現(xiàn)負(fù)載均衡,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03