JDBC:Java获取MySQL8.0数据库连接的五种方法—超全
如果着急可以直接看最后一个终极版
废话不多说,所有需要注意的点和步骤都在代码里
package com.atWYQ.jdbc;
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 connectTest {
//与数据库连接
//方式一:
@Test
public void test01() throws SQLException {
//获取Driver实现类的对象
Driver driver = new com.mysql.jdbc.Driver();
//这是MySQL8.0新的url连接,需要设置时区,该例中数据库名为test,可以根据实际情况更改
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8&serverTimezone=Hongkong";
//将用户名和密码放在Properties中
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123456");
//获取连接
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
//方式二: 反射
@Test
public void test02() throws Exception {
//1、获取Driver的实现类对象,使用反射
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2、提供连接数据的必要信息
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8&serverTimezone=Hongkong";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123456");
//3、获取连接
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
//方式三: 使用DriverManager,替换Driver
@Test
public void test03() throws Exception{
//1、获取Driver的实现类对象
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2、提供连接数据库的必要信息
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8&serverTimezone=Hongkong";
String user = "root";
String password = "123456";
//注册驱动
DriverManager.registerDriver(driver);
//获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//方式四:对三进行优化
@Test
public void test04() throws Exception{
//1、将com.mysql.jdbc.Driver加载进内存,它的静态代码块会自动注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、提供连接数据库的必要信息
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8&serverTimezone=Hongkong";
String user = "root";
String password = "123456";
//获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//方式五:最终版,通过加载配置文件进行连接
@Test
public void test05() throws Exception {
//1、读取配置文件的基本信息
InputStream is = connectTest.class.getClassLoader().getResourceAsStream("com/atWYQ/jdbc/jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driver = pros.getProperty("driver");
//2、加载驱动
Class.forName(driver);
//3、获取链接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
}
配置文件内容
user=root
password=123456
url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8&serverTimezone=Hongkong
driver=com.mysql.cj.jdbc.Driver
其中配置文件需建立在相应的位置,如果用resource文件存放的话,可以不用写路径
获取连接后如果需增、删、改、查操作,推荐阅读:JDBC:Java对MySQL8.0数据库通用的增、删、改、查操作