Jdbc连接mysql的五种连接方式
生活随笔
收集整理的這篇文章主要介紹了
Jdbc连接mysql的五种连接方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:五種連接方式 直接上碼
package com.wyjedu.jdbc;import com.mysql.jdbc.Driver;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties;/*** 測試連接方式*/public class Demo_jdbc02 {public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {//測試連接方式一:connect01();//測試連接方式二:connect02();//測試連接方式三:connect03();//測試連接方式四:connect04();//測試連接方式五connect05();}//1.第一種連接方式 直接創建driver 對象public static void connect01() throws SQLException {//(1):注冊驅動:Driver driver = new Driver();//(2):得到連接String url = "jdbc:mysql://localhost:3306/my_jdbc";Properties properties = new Properties();//獲取用戶 密碼properties.setProperty("user","root");properties.setProperty("password","wyj");//開始連接Connection connect = driver.connect(url, properties);System.out.println("連接方式一 = " + connect);connect.close();}// 第二種連接方式 通過反射來連接public static void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {//(1):注冊驅動//反射加載Driver類,創建它的Class對象,動態加載,更加靈活,減少依賴性Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");// 實例化aclass的一個對象Driver driver = (Driver) aClass.newInstance();//(2):得到連接String url = "jdbc:mysql://localhost:3306/my_jdbc";Properties properties = new Properties();properties.setProperty("user","root");properties.setProperty("password","wyj");Connection connect = driver.connect(url, properties);System.out.println("方式二 = " + connect);connect.close();}//第三種連接方式 使用DriverManger 替代 driver 進行統一管理public static void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {//(1):注冊驅動Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");Driver driver = (Driver) aClass.newInstance();//注冊驅動 driverDriverManager.registerDriver(driver);//(2):得到連接String url = "jdbc:mysql://localhost:3306/my_jdbc";//可以 不用創建 propertyString user = "root";String password = "wyj";Connection connection = DriverManager.getConnection(url, user, password);System.out.println("第三種連接方式 = " + connection);}//4.使用 Class.forName 自動完成注冊驅動,(其driver 源碼當中有解釋)public static void connect04() throws ClassNotFoundException, SQLException {//(1):注冊驅動Class.forName("com.mysql.jdbc.Driver");/*** Driver* 源碼:static {* try {* 注意這里是和方式三當中我們寫的是一樣的,也就是自動完成注冊* DriverManager.registerDriver(new Driver());* } catch (SQLException var1) {* throw new RuntimeException("Can't register driver!");* }* }**///(2):得到連接String url = "jdbc:mysql://localhost:3306/my_jdbc";String user = "root";String password = "wyj";Connection connection = DriverManager.getConnection(url, user, password);System.out.println("第四種連接方式 = " + connection);connection.close();}//5.DriverManager getConnection ("jdbc: mysql: //localhost: 3306/testdb","root//"root");中的字符串各個值,比如端口,數據庫,用戶名,密碼為了方便,我們可以將信息//寫入到 properties文件中,方便操作//這樣在連接不同的數據庫時,可以直接修改配置文件,而不用修改源碼public static void connect05() throws IOException, ClassNotFoundException, SQLException {//通過Properties獲取配置文件的信息Properties properties = new Properties();properties.load(new FileInputStream("src//mysql.properitys"));//獲取配置文件的相關信息String user = properties.getProperty("user");String password = properties.getProperty("password");String driver = properties.getProperty("driver");String url = properties.getProperty("url");//(1):注冊驅動(可以不用寫 在導入的jar包下 有相關的配置文件信息 會自動加載,但你一般還是會寫上 這樣可以清楚知道,注冊是誰的數據庫)Class.forName(driver);Connection connection = DriverManager.getConnection(url, user, password);System.out.println("連接方式五 = "+connection);connection.close();}}如有疑問,請留言
總結
以上是生活随笔為你收集整理的Jdbc连接mysql的五种连接方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1017 The Best Peak S
- 下一篇: 满汉楼(德鲁伊连接池+DBUtils+D