JDBC的各种连接方式
JDBC的各种连接方式
终极版
// 最终版
// 将配置写在配置文件中,用流读取
@Test
public void finalTestConnection() throws Exception {
// 读取配置文件
Properties properties = new Properties();
// 拿到配置文件的流
FileInputStream is = new FileInputStream("src/jdbc.properties");
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url,user,password);
System.out.println(connection);
}
配置文件
driver=com.mysql.jdbc.Driver
user=root
password=coderDreams
url=jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
原始方式1
// 调用接口的方法
@Test
public void testConnection01() throws SQLException {
// 拿到Driver接口对象
Driver driver = new com.mysql.jdbc.Driver();
// 调用方法拿到Connection
String url = "jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","coderDreams");
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
方式2
// 改变拿到接口对象的方法,提高可扩展性
@Test
public void testConnection02() throws Exception {
// 拿到Driver接口对象
Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
// 调用方法拿到Connection
String url = "jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","coderDreams");
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
方式3
// 使用DriverManager替换Driver
@Test
public void testConnection03() throws Exception {
// 拿到Driver接口对象
Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
// 注册驱动
DriverManager.registerDriver(driver);
String url = "jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","coderDreams");
Connection connection = DriverManager.getConnection(url, info);
System.out.println(connection);
}
方式4(常用)
// 发现类加载时会自动注册
@Test
public void testConnection04() throws Exception {
// 拿到Driver接口对象
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","coderDreams");
Connection connection = DriverManager.getConnection(url, info);
System.out.println(connection);
}
方式5(了解)
// 发现驱动加载时会自动注册
@Test
public void testConnection05() throws Exception {
// 省略了类加载
// 但是不推荐,降低了扩展性
String url = "jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","coderDreams");
Connection connection = DriverManager.getConnection(url, info);
System.out.println(connection);
}