栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Jdbc连接mysql的五种连接方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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");
        

        //(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();


    }




}

如有疑问,请留言

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/281886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号