Jdbc连接mysql的五种连接方式

一:五种连接方式 直接上码

package com.wyjedu.jdbc;


import com.mysql.jdbc.Driver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 测试连接方式
 */

public class Demo_jdbc02 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {

        //测试连接方式一:
        connect01();

        //测试连接方式二:
        connect02();

        //测试连接方式三:
        connect03();

        //测试连接方式四:
        connect04();

        //测试连接方式五
        connect05();


    }
    //1.第一种连接方式 直接创建driver 对象
    public static void connect01() throws SQLException {

        //(1):注册驱动:
        Driver driver = new Driver();

        //(2):得到连接

        String url = "jdbc:mysql://localhost:3306/my_jdbc";

        Properties properties = new Properties();

        //获取用户 密码
        properties.setProperty("user","root");
        properties.setProperty("password","wyj");

        //开始连接
        Connection connect = driver.connect(url, properties);

        System.out.println("连接方式一 = " + connect);

        connect.close();

    }

    // 第二种连接方式 通过反射来连接

    public static void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //(1):注册驱动
        //反射加载Driver类,创建它的Class对象,动态加载,更加灵活,减少依赖性
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");

        // 实例化aclass的一个对象
        Driver driver = (Driver) aClass.newInstance();

        //(2):得到连接

        String url = "jdbc:mysql://localhost:3306/my_jdbc";

        Properties properties = new Properties();

        properties.setProperty("user","root");
        properties.setProperty("password","wyj");

        Connection connect = driver.connect(url, properties);

        System.out.println("方式二 = " + connect);

        connect.close();
    }

    //第三种连接方式 使用DriverManger  替代 driver  进行统一管理

    public static void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        //(1):注册驱动
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

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

        //(2):得到连接

        String url = "jdbc:mysql://localhost:3306/my_jdbc";

        //可以 不用创建 property
        String user = "root";
        String password = "wyj";

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

        System.out.println("第三种连接方式 = " + connection);

    }

    //4.使用 Class.forName 自动完成注册驱动,(其driver 源码当中有解释)

    public static void connect04() throws ClassNotFoundException, SQLException {

        //(1):注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        /**
         * Driver
         * 源码:static {
         *         try {
         *             注意这里是和方式三当中我们写的是一样的,也就是自动完成注册
         *             DriverManager.registerDriver(new Driver());
         *         } catch (SQLException var1) {
         *             throw new RuntimeException("Can't register driver!");
         *         }
         *     }
         *
         */

        //(2):得到连接

        String url = "jdbc:mysql://localhost:3306/my_jdbc";
        String user = "root";
        String password = "wyj";

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

        System.out.println("第四种连接方式 = " + connection);

        connection.close();

    }

    //5.DriverManager getConnection ("jdbc: mysql: //localhost: 3306/testdb","root
    //"root");中的字符串各个值,比如端口,数据库,用户名,密码为了方便,我们可以将信息
    //写入到 properties文件中,方便操作
    //这样在连接不同的数据库时,可以直接修改配置文件,而不用修改源码

    public static void connect05() throws IOException, ClassNotFoundException, SQLException {

        //通过Properties获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src//mysql.properitys"));

        //获取配置文件的相关信息

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

        //(1):注册驱动(可以不用写  在导入的jar包下  有相关的配置文件信息 会自动加载,但你一般还是会写上 这样可以清楚知道,注册是谁的数据库)
        Class.forName(driver);

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

        System.out.println("连接方式五 = "+connection);

        connection.close();


    }




}

如有疑问,请留言

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天天向上的菜鸡杰!!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值