jdbc的幾種連線方式
阿新 • • 發佈:2021-11-13
1、獲取Driver實現類物件
public void conn1() throws SQLException { //註冊驅動 Driver driver = new Driver(); //獲取連線 String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"; Properties properties = new Properties(); properties.setProperty("user", "root"); properties.setProperty("password", "1234"); Connection connection = driver.connect(url, properties); System.out.println(connection); }
2、利用反射機制
public void conn2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException { Class<?> aClass = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver)aClass.newInstance(); String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"; Properties properties = new Properties(); properties.setProperty("user", "root"); properties.setProperty("password", "1234"); Connection connection = driver.connect(url, properties); System.out.println(connection); }
3、使用DriverManager替換Driver
public void conn3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException { Class<?> aClass = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver)aClass.newInstance(); String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"; String user = "root"; String password = "1234"; DriverManager.registerDriver(driver); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
4、使用Class.forName()自動完成註冊驅動
public void conn4() throws ClassNotFoundException, SQLException {
//Class.forName()自動完成註冊驅動
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String user = "root";
String password = "1234";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
Driver原始碼
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
public Driver() throws SQLException {
}
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}
5、使用配置檔案,連線資料庫更加靈活
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
jdbc.username=root
jdbc.password=1234
public void conn5() throws ClassNotFoundException, IOException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream(new File("src\\jdbc.properties")));
String user = properties.getProperty("jdbc.user");
String password = properties.getProperty("jdbc.password");
String driver = properties.getProperty("jdbc.driver");
String url = properties.getProperty("jdbc.url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}