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,不再需要顯示調用class.forName()注冊驅動而是自動調用驅動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配置文件內容如下
url=jdbc:mysql://localhost:3306/test user=root password=123456
連接mysql程序
public void connection05() throws SQLException, ClassNotFoundException, IOException { // 使用Properties讀取配置文件下的內容 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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何使用Resttemplate和Ribbon調用Eureka實現(xiàn)負載均衡
這篇文章主要介紹了如何使用Resttemplate和Ribbon調用Eureka實現(xiàn)負載均衡,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03