JDBC:Java获取MySQL8.0数据库连接的五种方法---超全

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数据库通用的增、删、改、查操作

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值