jdbc的几种连接方式
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.username");
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);
}