赞
踩
- package day38;
-
- import com.mysql.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;
-
- /**
- * @Author:monian
- * @Wo yi wu ta,wei shou shu er!
- * @Date:2023/11/14 12:09
- */
- public class JdbcConn {
- @SuppressWarnings({"all"})
- @Test
- public void connect01() throws SQLException {
- // 方式1
- Driver driver = new Driver();
- String url = "jdbc:mysql://localhost:3306/mn_db02";
- // 将用户名和密码放入到Properties对象
- Properties properties = new Properties();
- properties.setProperty("user", "root");
- properties.setProperty("password", "chen");
- Connection connect = driver.connect(url, properties);
- System.out.println("方式1" + connect);
- }
-
- @Test
- //方式2
- public void connect02() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
- //使用反射加载Driver类
- Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
- Driver driver = (Driver) aClass.newInstance();
-
- String url = "jdbc:mysql://localhost:3306/mn_db02";
- // 将用户名和密码放入到Properties对象
- Properties properties = new Properties();
- properties.setProperty("user", "root");
- properties.setProperty("password", "chen");
-
- Connection connect = driver.connect(url, properties);
- System.out.println("方式2=" + connect);
- }
-
- @Test
- //方式3 使用DriverManager 替代 Driver 进行统一管理
- public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
- Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
- Driver driver = (Driver) aClass.newInstance();
-
- //创建url 和 user 和 password
- String url = "jdbc:mysql://localhost:3306/mn_db02";
- String user = "root";
- String password = "chen";
-
- DriverManager.registerDriver(driver);
-
- Connection connection = DriverManager.getConnection(url, user, password);
- System.out.println("方式3=" + connection);
- }
-
- @Test
- /**方式4: 使用Class.forName 自动完成注册驱动,简化代码
- * 推荐指数:*****
- */
- 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.jdbc.Driver"); // 删除也可以。建议写上
- //创建url 和 user 和 password
- String url = "jdbc:mysql://localhost:3306/mn_db02";
- String user = "root";
- String password = "chen";
- Connection connection = DriverManager.getConnection(url, user, password);
-
- System.out.println("方式4~" + connection);
- }
-
- @Test
- //推荐指数:****** 代码简洁+反射机制
- //方式5 ,在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
- 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 url = properties.getProperty("url");
- String driver = properties.getProperty("driver");
-
- Class.forName(driver);//建议写上
-
- Connection connection = DriverManager.getConnection(url, user, password);
-
- System.out.println("方式5=" + connection);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。