【后端开发】JDBC获取数据库连接的方式

一共有六种方式,但是不是并列关系,而是迭代关系

最后一种方式最好,新建一个jdbc.properties配置文件,通过最后一种方式读取配置文件连接数据库

找到MySQL版本对应的connection的包就可以不用听别人的

https://mvnrepository.com/artifact/mysql/mysql-connector-java

通过看源代码可以发现现在的包名加个了.cj,其实区别还是不大

下面的代码还是很好理解的除了已经被我忘掉的反射,记得复习一下

不要想了,下面的密码都是我修改过的了

package com.cls1277.connection;

import org.junit.Test;

import java.io.IOException;
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 connectionTest1() throws SQLException {
        Driver driver = new com.mysql.cj.jdbc.Driver();
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "xxx");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    @Test
    public void connectionTest2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class clas = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clas.newInstance();
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "xxx");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    @Test
    public void connectionTest3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class clas = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clas.newInstance();
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "xxx";
        DriverManager.registerDriver(driver);
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    @Test
    public void connectionTest4() throws ClassNotFoundException, SQLException {
        //mysql提供的驱动的类中有静态代码块,在加载类的时候执行,因此直接new了一个新的对象
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "xxx";
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    @Test
    public void connectionTest5() throws SQLException {
        //最好第4种,那句话不要省,只是mysql可以省而已
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "xxx";
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    @Test
    public void connectTest6() throws IOException, ClassNotFoundException, SQLException {
        InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(resourceAsStream);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverclass = pros.getProperty("driverClass");
        Class.forName(driverclass);
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cls1277

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值