Here are five ways to connect java to a database.
@Test
If you find it is red, Alt + enter.
Here I choose the JUnit 5.8.1.
Wait for a while.
Then you can find the Test (org.junit.jupiter.api).
1.
@Test
public static void main(String[] args) throws SQLException {
Driver driver = new Driver();
String url = "jdbc:mysql://localhost:3306/j_db01";
//将 用户名和密码放入到Properties 对象
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "123456");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
2.
//方式2
@Test
public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/j_db01";
//将 用户名和密码放入到Properties 对象
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "123456");
Connection connect = driver.connect(url, properties);
System.out.println("方式2=" + connect);
}
3.
//方式3 使用DriverManager 替代Driver
@Test
public void connect03() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Class> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//创建url 和user 和password
String url = "jdbc:mysql://localhost:3306/j_db01";
String user = "root";
String password = "123456";
DriverManager.registerDriver(driver); //注册Driver驱动
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第三种方式=" + connection);
}
4.
//方式4:使用Class.forName 自动完成注册驱动,简化代码
@Test
public void connect04() throws ClassNotFoundException, SQLException {
//使用反射加载了Driver类
//在加载Driver类时,完成注册
/*
源码:1.静态代码块,在类加载时,会执行一次
2.DriverManager.registerDriver(new Driver());
3.因此注册driver的工作已经完成
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
*/
Class.forName("com.mysql.cj.jdbc.Driver");
// 创建url 和user 和password
String url = "jdbc:mysql://localhost:3306/j_db01";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第4种方式=" + connection);
}
5.
//方式5,在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
@Test
public void connect05() throws IOException, ClassNotFoundException, SQLException {
//通过Properties对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
//获取相关的值
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
// Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式5 " + connection);
}
Here is all the java code:
package com.jdbc;
import com.mysql.cj.jdbc.Driver;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* java 连接mysql 的五种方式
*/
public class JdbcConn {
@Test
public static void main(String[] args) throws SQLException {
Driver driver = new Driver();
String url = "jdbc:mysql://localhost:3306/j_db01";
//将 用户名和密码放入到Properties 对象
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "123456");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
//方式2
@Test
public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/j_db01";
//将 用户名和密码放入到Properties 对象
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "123456");
Connection connect = driver.connect(url, properties);
System.out.println("方式2=" + connect);
}
//方式3 使用DriverManager 替代Driver
@Test
public void connect03() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Class> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//创建url 和user 和password
String url = "jdbc:mysql://localhost:3306/j_db01";
String user = "root";
String password = "123456";
DriverManager.registerDriver(driver); //注册Driver驱动
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第三种方式=" + connection);
}
//方式4:使用Class.forName 自动完成注册驱动,简化代码
//这种方式方式获取连接是使用的最多,推荐使用
@Test
public void connect04() throws ClassNotFoundException, SQLException {
//使用反射加载了Driver类
//在加载Driver类时,完成注册
/*
源码:1.静态代码块,在类加载时,会执行一次
2.DriverManager.registerDriver(new Driver());
3.因此注册driver的工作已经完成
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
*/
Class.forName("com.mysql.cj.jdbc.Driver");
// 创建url 和user 和password
String url = "jdbc:mysql://localhost:3306/j_db01";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第4种方式=" + connection);
}
//方式5,在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
@Test
public void connect05() throws IOException, ClassNotFoundException, SQLException {
//通过Properties对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
//获取相关的值
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
// Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式5 " + connection);
}
}
mysql.properties
Run