【JDBC】连接数据库的几种方式

46 篇文章 1 订阅
2 篇文章 0 订阅

 以mysql连接为例

引入驱动

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
    <!--<scope>runtime</scope>-->
</dependency>

 junit泰马

package xyz.hashdog.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {


    /**
     * 1.通过驱动实现类获取
     * @throws SQLException
     */
    @Test
    public  void  testConnection1() throws SQLException {
        Driver driver = new com.mysql.jdbc.Driver();
        String url = "jdbc:mysql://localhost:3306/st-jdbc";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","hashdog2019");
        Connection conn = driver.connect(url,info);
        System.out.println(conn);

    }

    /**
     * 2.通过反射获取
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws SQLException
     */
    @Test
    public void testConnection2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //反射方式获取Driver实现类,可以根据驱动包名切换
        Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)clazz.newInstance();
        String url = "jdbc:mysql://localhost:3306/st-jdbc";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","hashdog2019");
        Connection conn = driver.connect(url,info);
        System.out.println(conn);

    }


    /**
     * 3.DriverManager替换Driver
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws SQLException
     */
    @Test
    public void testConnection3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //1.获取Driver实现类
        Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)clazz.newInstance();
        //2.提供基本信息
        String url = "jdbc:mysql://localhost:3306/st-jdbc";
        String user = "root";
        String password = "hashdog2019";

        //3.注册驱动
        DriverManager.registerDriver(driver);
        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);

    }


    /**
     * 4.优化方式三,加载驱动会自动注册
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws SQLException
     */
    @Test
    public void testConnection4() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //1.提供基本信息
        String url = "jdbc:mysql://localhost:3306/st-jdbc";
        String user = "root";
        String password = "hashdog2019";

        //2.获取Driver实现类
        Class.forName("com.mysql.jdbc.Driver");
//        Driver driver = (Driver)clazz.newInstance();
//        DriverManager.registerDriver(driver);
        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);

    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值