当前位置:   article > 正文

Java连接Mysql数据库的5种方法_java如何连接mysql数据库

java如何连接mysql数据库

参考资料:https://www.bilibili.com/video/av69222697?p=63

前言

本文讲述了Java连接Mysql数据库的5种方法,其中最后一种方式是需要掌握的。

1 使用第三方库Driver

@Test
    public void connectionTest01() throws SQLException {
        // 获取Driver实现类对象
        Driver driver = new com.mysql.jdbc.Driver();
        // jdbc:mysql :协议
        // localhost: ip地址
        // 3306 :mysql端口号
        // test :数据库名
        String url = "jdbc:mysql://10.211.55.3:3306/test";
        // 将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "root");

        Connection conn = driver.connect(url, info);
        // 如果打印成功,说明连接成功
        System.out.println(conn);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

第一种方式用到了第三方库com.mysql.jdbc.Driver,这个包是需要去mysql官网下载、导入的。

2 使用反射

使用反射实例化Driver,不在代码中体现第三方数据库的API,使得程序具有良好的移植性,体现了面向接口编程的思想。

@Test
    public void connectionTest02() throws Exception{
        // 1.获取Driver实现类对象:使用反射
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver con = (Driver) clazz.newInstance();

        // 2.mysql的地址,用户名和密码
        String url = "jdbc:mysql://10.211.55.3:3306/test";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");

        Connection connect = con.connect(url, info);
        System.out.println(connect);

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3 使用DriverManager

使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。

 /**
     * 创建数据库连接的第三中方式
     * 使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。
     */
    @Test
    public void connectiontest03() throws Exception {
        // 1.数据库连接四要素
        String url = "jdbc:mysql://localhost:3307/test?autoReconnect=true&useSSL=false";
        String username = "root";
        String password = "root";
        String driverName = "com.mysql.jdbc.Driver";

        // 2.实例化Driver
        Class clazz = Class.forName(driverName);
        Driver o = (Driver) clazz.newInstance();

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

        // 获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println(connection);

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

4 自动注册

driver.class类中的静态代码块在类加载的时候被执行,实现了自动注册。相比方式3免去了自动注册
在这里插入图片描述

 @Test
    public void connectionTest04() throws Exception{
        // 1.数据库连接四要素
        String url = "jdbc:mysql://localhost:3307/test?useSSL=false";
        String username = "root";
        String password = "root";
        String driverName = "com.mysql.jdbc.Driver";
        
        // 将Driver类加载到内存中
        Class.forName(driverName);

        // 2.实例化Driver
//        Class clazz = Class.forName(driverName);
//        Driver o = (Driver) clazz.newInstance();
        // 3.注册驱动
//        DriverManager.registerDriver(o);
        //以上代码被注释的原因:在Driver.class中有如下代码,
        // 在类加载的时候静态代码块被执行
        /**
         * static {
         *                 try {
         *                     DriverManager.registerDriver(new Driver());
         *                 } catch (SQLException var1) {
         *                     throw new RuntimeException("Can't register driver!");
         *                 }
         *             }
         */

        // 获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println(connection);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

5 使用配置文件

在方式四的基础上使用配置文件的方式保存配置信息(user,password,url,driverClass),在代码中加载配置文件。
在IDEA的相对路径下创建jdbc.properites
在这里插入图片描述

user=root
password=root
url=jdbc:mysql://localhost:3307/test/?useSSL=false
driverName=com.mysql.jdbc.Driver
  • 1
  • 2
  • 3
  • 4
 @Test
    public void connectionTest05() throws Exception {
        // 1.加载配置文件
        InputStream ras = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties props = new Properties();
        props.load(ras);

        // 2.读取配置文件信息
        String user= props.getProperty("user");
        String password= props.getProperty("password");
        String url= props.getProperty("url");
        String driverClass= props.getProperty("driverClass");

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

        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

使用配置文件的好处:

①实现了代码和数据的分离,如果需要修改配置信息,直接在配置文件中修改,不需要深入代码
②如果修改了配置信息,省去重新编译的过程。

最后一种方式需要掌握,前四种方式了解即可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/852735
推荐阅读
相关标签
  

闽ICP备14008679号