JAVA获取数据库连接的五种方式

本文详细介绍了使用Java连接MySQL数据库的五种方法,包括直接实例化Driver、反射加载Driver、使用DriverManager注册Driver以及从配置文件读取连接信息等。每种方法都包含关键步骤和代码示例,旨在帮助开发者灵活地建立数据库连接。
摘要由CSDN通过智能技术生成

JAVA获取数据库连接的五种方式

package Chapter19Jdbc.JdbcConnection;

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;

/**
 * Written by 3778
 * 连接数据库的五种方式
 */
public class Demo {
    public static void main(String[] args) {

    }

    @Test
    public void methodOne() throws SQLException {
        //注册驱动
        Driver driver = new Driver();

        //得到连接
        String url = "jdbc:mysql://localhost:3306/db03";

        //将用户名和密码放入到properties对象
        Properties properties = new Properties();
        //user和password是固定格式意为用户名与密码
        properties.setProperty("user", "root");
        properties.setProperty("password", "lyc");

        //连接数据库
        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }

    @Test
    public void methodTwo() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        //通过反射加载Driver类(动态加载,更加灵活,减少依赖性)
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //得到连接
        String url = "jdbc:mysql://localhost:3306/db03";

        //将用户名和密码放入到properties对象
        Properties properties = new Properties();
        //user和password是固定格式意为用户名与密码
        properties.setProperty("user", "root");
        properties.setProperty("password", "lyc");

        //连接数据库
        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }

    @Test
    public void methodThree() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //通过反射加载Driver类(动态加载,更加灵活,减少依赖性)
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //得到连接
        String url="jdbc:mysql://localhost:3306/db03";
        String user="root";
        String password="lyc";

        //注册Driver驱动(使用DriverManage替代Driver进行统一管理,扩展性更优)
        DriverManager.registerDriver(driver);

        Connection connection=DriverManager.getConnection(url,user,password);
        System.out.println(connection);

    }

    @Test
    public void methodFour() throws ClassNotFoundException, SQLException {  //方式3的简化版
        //通过反射加载Driver类(动态加载,更加灵活,减少依赖性)
        //在加载Driver类时,源码自动完成注册
        Class.forName("com.mysql.jdbc.Driver"); //mysql驱动5.1.6以及更高的驱动版本可以无需调用Class.forName(),自动调用;

        //得到连接
        String url="jdbc:mysql://localhost:3306/db03";
        String user="root";
        String password="lyc";

        Connection connection=DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }

    @Test
    public void methodFive() throws IOException, ClassNotFoundException, SQLException {
        //在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql5_7.properties"));
        //获取相关信息
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);
        Connection connection=DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Zen1

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

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

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

打赏作者

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

抵扣说明:

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

余额充值