Java mysql JDBC



import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.security.PublicKey;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @Author HLY
 * @Create 2019-12-04 17:13
 */
public class ConnectionTest {

    //方式一
    @Test
    public void test() throws SQLException {
        Driver driver=new com.mysql.cj.jdbc.Driver();
        String s ="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai" ;
        Properties info=new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");
        Connection connect = driver.connect(s, info);
        System.out.println(connect);
    }

    //方式二
    @Test
    public void test1() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver= (Driver) clazz.newInstance();

        String s ="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai" ;
        Properties info=new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");
        Connection connect = driver.connect(s, info);
        System.out.println(connect);
    }

    //方式三
    @Test
    public void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //获取Driver实现的对象
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver= (Driver) clazz.newInstance();
        //注册驱动
        DriverManager.registerDriver(driver);
        //获取链接
        String s ="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai" ;
        Connection connection = DriverManager.getConnection(s, "root", "root");
        System.out.println(connection);

    }


    //方式四:只是加载驱动,不用显示的注册驱动
    @Test
    public void test3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取链接
        String s ="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai" ;
        Connection connection = DriverManager.getConnection(s, "root", "root");
        System.out.println(connection);

    }

    //方式五:配置文件方式
    @Test
    public void test4() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException, IOException {
        InputStream inputStream=ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");

        Properties properties=new Properties();
        properties.load(inputStream);

        String user=properties.getProperty("user");
        String password=properties.getProperty("password");
        String url=properties.getProperty("url");
        String driverClass=properties.getProperty("driverClass");

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

멋진

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

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

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

打赏作者

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

抵扣说明:

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

余额充值