Java获取数据库连接的方式有五种
方法一
方法一 : 通过实现类com.mysql.cj.jdbc.Driver() 实现 java.sql.Driver方式
步骤:
1): 获取驱动 java.sql.Driver 并且使用实现类 com.mysql.cj.jdbc.Driver 实例化
2): 准备驱动连接条件,①url 连接数据库地址及数据库名;②配置文件,(所连数据库用户名,密码)
3); 驱动连接
代码实现方式一
@Test
public void testConnection1() throws SQLException {
Driver driver = null;
driver = new com.mysql.cj.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123456");
Connection connection = null;
connection = driver.connect(url, info);
System.out.println(connection);
}
方法二
方法二 使用的是反射动态获取数据库驱动的实现类,其他步骤和第一步差不多,
步骤:
1)动态获取数据库驱动实现类
2)将java.sql.Driver驱动实例化
3)提供连接所需条件,url,配置文件(所连数据库用户名,密码)
4)驱动连接
代码实现方式二
@Test
public void testConnection2() throws Exception {
Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123456");
Connection connect = driver.connect("jdbc:mysql://localhost:3306/test?serverTimezone=UTC", info);
System.out.println(connect);
}
方法三
方法三使用DriverManager注册驱动
步骤:
1)动态获取数据库实现类
2)将java.sql.Driver实例化
3)使用DriverManager.registerDriver()方法注册驱动
4)准备连接条件,url,配置文件
5)使用DriverManager.getConnection()方法连接
代码实现方法三
@Test
public void testConnection3() throws Exception {
Connection connection = null;
Class driverClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) driverClass.getDeclaredConstructor().newInstance();
// register driver
DriverManager.registerDriver(driver);
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC", "root", "123456");
if (connection != null) {
System.out.println("succeed connecting!");
}
}
方法四
方法四简化了方法三:直接去除注册那一步,因为在数据库实现类的Driver驱动中,会自动注册驱动
步骤:
1)获取实现类驱动
2)准备连接条件,url,配置文件
3)驱动连接
代码实现方法四
@Test
public void testConnection4() throws Exception {
Connection connection = null;
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC", "root", "123456");
if (connection != null) {
System.out.println("succeed connecting!");
}
}
方法五
方法五使用的是读取配置文件,减少了重要信息,比如用户名,密码等的暴露,
将配置文件存储在 .properties 的文件中,然后实用类加载器加载到配置对象里面,在从配置对象里面获取所需信息的变量名,
步骤:
1)将配置信息存储到 .properties 的文件中, 配置文件尽量放在src下,例:
2)使用类加载器加载到流中 InputStream is = TestConnection.class.getClassLoader().getResourceAsStream(“mysql-jdbc.properties”);
3)建立一个配置文件对象,将此流中的加载进去
Properties pro = new Properties(); pro.load(is);
4)使用pro.getProperty(“属性名”) 得到连接所需要的条件 例如:
String user = pro.getProperty(“user”); …
5)获取数据库驱动实现类
6)使用DriverManager.getConnection(driver); 连接
代码实现方法五
@Test
public void review2() throws Exception {
// 1) read properties file into stream
InputStream is = TestConnection.class.getClassLoader().getResourceAsStream("mysql-jdbc.properties");
// 2) load stream message into properties object
Properties prop = new Properties();
prop.load(is);
// 3) use .getProperty("property") to get value
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
String driverClass = prop.getProperty("driverClass");
// 4) dynamic get database driver implements class
Class.forName(driverClass);
// 5) connect
Connection connection = DriverManager.getConnection(url, user, password);
if (connection != null) {
System.out.println("succeed connecting");
}
}