美文网首页
Java使用JDBC连接MySQL方法详解

Java使用JDBC连接MySQL方法详解

作者: 3823丶 | 来源:发表于2020-02-01 17:28 被阅读0次

    1. 概述

    Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是Sun Microsystems的商标。我们通常说的JDBC是面向关系型数据库的。
      JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据。

    执行流程:

    • 连接数据源,如:数据库。
    • 为数据库传递查询和更新指令。
    • 处理数据库响应并返回的结果。

    2. JDBC 架构

    分为双层架构和三层架构。   
    双层    

    双层    
    作用:此架构中,Java Applet 或应用直接访问数据源。
    条件:要求 Driver 能与访问的数据库交互。
    机制:用户命令传给数据库或其他数据源,随之结果被返回。
    部署:数据源可以在另一台机器上,用户通过网络连接,称为 C/S配置(可以是内联网或互联网)。   
    三层 
    三层    
    侧架构特殊之处在于,引入中间层服务。
    流程:命令和结构都会经过该层。
    吸引:可以增加企业数据的访问控制,以及多种类型的更新;另外,也可简化应用的部署,并在多数情况下有性能优势。
    历史趋势: 以往,因性能问题,中间层都用 C 或 C++ 编写,随着优化编译器(将 Java 字节码 转为 高效的 特定机器码)和技术的发展,如EJB,Java 开始用于中间层的开发这也让 Java 的优势突显出现出来,使用 Java 作为服务器代码语言,JDBC随之被重视。

    3. JDBC使用详解

    3.1 通过使用第三方类实现

    @Test
    public void testConnection1() throws SQLException {
        Driver driver = new com.mysql.jdbc.Driver();
    
        String url = "jdbc:mysql://localhost:3306/test";
    
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "root");
            
        Connection conn = driver.connect(url, info);
            
        System.out.println(conn.toString());
    }
    

    3.2 使用反射机制加载驱动

    @Test
    public void testConnection2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
            
            
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "root");
            
        Connection conn = driver.connect(url, info);
            
        System.out.println(conn.toString());
    }
    

    3.3 使用DriverManager类优化代码

    @Test
    public void testConnection3() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
            
        String url = "jdbc:mysql://127.0.0.1:3306/test";
        String user = "root";
        String password = "root";
            
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
            
        System.out.println(connection);
    }
    

    3.4 MySQL驱动使用静态代码块自动加载

    @Test
    public void testConnection3() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
            
        String url = "jdbc:mysql://127.0.0.1:3306/test";
        String user = "root";
        String password = "root";
            
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
            
        System.out.println(connection);
    }
    

    MySQL静态源码如下:

    MySQL静态源码

    3.5 终极配置文件版

    优点:
    1、实现了数据与代码的分离,是一种解耦的方式;
    2、可移植性大大增强;
    3、如果修改配置信息,可以避免程序重新打包;

    jdbc.properties文件

    user=root
    password=root
    url=jdbc:mysql://localhost:3306/test
    driverClass=com.mysql.jdbc.Driver
    
    @Test
    public void testConnection5() throws IOException, ClassNotFoundException, SQLException {
        InputStream resource = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            
        Properties properties = new Properties();
        properties.load(resource);
            
        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);
    }
    

    3.6 完整代码

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.sql.Connection;
    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    import org.junit.Test;
    
    public class ConnectionTest {
        @Test
        public void testConnection1() throws SQLException {
            
            Driver driver = new com.mysql.jdbc.Driver();
            
            String url = "jdbc:mysql://localhost:3306/test";
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            
            Connection conn = driver.connect(url, info);
            
            System.out.println(conn.toString());
        }
        
        @Test
        public void testConnection2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
            
            
            String url = "jdbc:mysql://localhost:3306/test";
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            
            Connection conn = driver.connect(url, info);
            
            System.out.println(conn.toString());
        }
        
        @Test
        public void testConnection3() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
            
            String url = "jdbc:mysql://127.0.0.1:3306/test";
            String user = "root";
            String password = "root";
            
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(url, user, password);
            
            System.out.println(connection);
        }
        
        @Test
        public void testConnection4() throws ClassNotFoundException, SQLException {
            
            Class.forName("com.mysql.jdbc.Driver");
            
            String url = "jdbc:mysql://127.0.0.1:3306/test";
            String user = "root";
            String password = "root";
            
            Connection connection = DriverManager.getConnection(url, user, password);
            
            System.out.println(connection);
        }
        
        @Test
        public void testConnection5() throws IOException, ClassNotFoundException, SQLException {
            InputStream resource = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            
            Properties properties = new Properties();
            properties.load(resource);
            
            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);
        }
    }
    

    相关文章

      网友评论

          本文标题:Java使用JDBC连接MySQL方法详解

          本文链接:https://www.haomeiwen.com/subject/pnjxxhtx.html