JdbcConnection五种实现方式

本文详细介绍了使用Java JDBC连接MySQL数据库的五种不同方式,包括直接实例化Driver、反射加载Driver、注册Driver后再连接、省略注册步骤以及从配置文件读取连接信息等方法。每种方法都包含关键步骤如设置URL、用户名和密码,并展示了如何获取Connection对象。
摘要由CSDN通过智能技术生成

JdbcConnection五种实现方式

package 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;

/**
 * @author 小邱
 * @version 0.0.1
 * @description JdbcTest
 * @since 2021/9/27 16:58
 */
public class JdbcConnectionTest {
    //获取连接方式一
    @Test
    public void getConncetion1() throws SQLException {
        //1.提供java.sql.Driver接口实现类的对象
        Driver driver = new com.mysql.jdbc.Driver();

        //2.提供url,指明具体操作的数据
        String url = "jdbc:mysql://localhost:3306/book";
        //3.提供Properties的对象,指明用户名和密码
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        //4.调用driver的connect(),获取连接
        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }
    //获取连接方式二
    @Test
    public void getConncetion2() throws Exception{
        //1.通过反射实例化Driver
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2.提供url,指明具体操作的数据
        String url = "jdbc:mysql://localhost:3306/book";
        //3.提供Properties的对象,指明用户名和密码
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        //4.调用driver的connect(),获取连接
        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }
    //获取连接方式三
    @Test
    public void getConncetion3() throws Exception{
        //1.数据库连接的基本要素
        //提供url,指明具体操作的数据库、登录用户和密码
        String url = "jdbc:mysql://localhost:3306/book";
        String user = "root";
        String password= "123456";

        //2.通过反射实例化Driver
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //3.注册驱动
        DriverManager.registerDriver(driver);

        //4.获取连接
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }
    //获取连接方式四
    @Test
    public void getConncetion4() throws Exception{
        //1.数据库连接的基本要素
        //提供url,指明具体操作的数据库、登录用户和密码
        String url = "jdbc:mysql://localhost:3306/book";
        String user = "root";
        String password= "123456";

        //2.通过反射实例化Driver
        Class.forName("com.mysql.cj.jdbc.Driver");
        //3.获取连接
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }
    //获取连接方式五(推荐)
    @Test
    public void getConncetion5() throws Exception{
        //1.加载配置文件
        InputStream resource = JdbcConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resource);

        //2.读取配置信息
        String user = properties.getProperty("username");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClassName");

        //3.加载驱动
        Class.forName(driverClass);

        //4.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);




    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值