最原始的连接方式
package com.daijunyi;
import org.junit.Test;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionTest {
// 方式一
@Test
public void connection() throws SQLException {
Driver driver = new com.mysql.cj.jdbc.Driver();
Properties properties = new Properties();
properties.put("user","root");
properties.put("password","qwerasdf123");
String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
// 方式三
@Test
public void connection1() throws Exception {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
Properties properties = new Properties();
properties.put("user","root");
properties.put("password","qwerasdf123");
String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
// 方式三
@Test
public void connection2() throws Exception{
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
DriverManager.registerDriver(driver);
Properties properties = new Properties();
properties.put("user","root");
properties.put("password","qwerasdf123");
String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
Connection connection = DriverManager.getConnection(url, properties);
System.out.println(connection);
}
// 方式四 我们只需要加载驱动到内存 ,因为驱动一加载类的时候 静态代码块会自动注册驱动
// */
// public class Driver extends NonRegisteringDriver implements java.sql.Driver {
// //
// // Register ourselves with the DriverManager
// //
// static {
// try {
// java.sql.DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
// } catch (SQLException E) {
// throw new RuntimeException("Can't register driver!");
// }
// }
@Test
public void connection3() throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
// Driver driver = (Driver) aClass.newInstance();
// DriverManager.registerDriver(driver);
Properties properties = new Properties();
properties.put("user","root");
properties.put("password","qwerasdf123");
String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
Connection connection = DriverManager.getConnection(url, properties);
System.out.println(connection);
}
@Test
public void connection4() throws Exception{
InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
}
url=jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8
user=root
password=qwerasdf123
driver=com.mysql.cj.jdbc.Driver