• JDBC-Connection


    新建Maven工程

    pom.xml

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <!-- 指定jdk -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    数据库信息

    driver=com.mysql.cj.jdbc.Driver
    jdbcUrl=jdbc:mysql://192.168.8.136:3306/jdbc
    user=root
    password=root

    获取链接

    Driver

    /**
     * Driver 是一个接口: 可以通过 Driver 的实现类对象获取数据库连接
     */
    @Test
    public void testDriver() throws SQLException {
        // 创建 Driver 实现类对象
        Driver driver = new com.mysql.cj.jdbc.Driver();
    
        // 准备连接数据库信息: url, user, password
        String url = "jdbc:mysql://192.168.8.136:3306/jdbc";
        Properties info = new Properties();
        info.put("user", "root");
        info.put("password", "root");
    
        // 调用 Driver 接口的 connect(url, info) 获取数据库连接
        Connection connection = driver.connect(url, info);
        
        System.out.println(connection);
    }
    
    @Test
    public void getConnection() throws Exception{
        // 读取类路径下的 jdbc.properties 文件
        InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);
    
        String driverClass = properties.getProperty("driver");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
    
        //通过反射 Driver 对象
        Driver driver = (Driver) Class.forName(driverClass).newInstance();
    
        Properties info = new Properties();
        info.put("user", user);
        info.put("password", password);
    
        //通过 Driver 的 connect 方法获取数据库连接
        Connection connection = driver.connect(jdbcUrl, info);
    
        System.out.println(connection);
    }

    DriverManager

    /**
     * DriverManager 是驱动的管理类
     * 1). 可以通过重载的 getConnection() 方法获取数据库连接. 较为方便
     * 2). 可以同时管理多个驱动程序: 若注册了多个数据库连接, 则调用 getConnection()
     * @throws Exception
     */
    @Test
    public void testDriverManager() throws Exception{
        // 准备连接数据库
        String driverClass = "com.mysql.cj.jdbc.Driver";
        String jdbcUrl = "jdbc:mysql://192.168.8.136/jdbc";
        String user = "root";
        String password = "root";
    
        // 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块
        Class.forName(driverClass);
        // 通过 DriverManager 的 getConnection() 方法获取数据库连接
        Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
        
        System.out.println(connection);
    }
    
    @Test
    public void getConnection2() throws Exception{
        Properties properties = new Properties();
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(in);
    
        String driver = properties.getProperty("driver");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
    
        // 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块)
        Class.forName(driver);
        // 通过 DriverManager 的 getConnection() 方法获取数据库连接.
        Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
        
        System.out.println(connection);
    }

    oracle官方JDBC文档

  • 相关阅读:
    C# 保存图片文件异常--文件名、目录名或卷标语法不正确。
    NPOI 导出Excel WPS格式正常 Office格式异常
    ionic 项目安装依赖出现以下错误
    关于升级npm 出现 “Refusing to delete C:UsersltAppDataRoaming pm px.cmd:”
    关于 NPOI 单元的样式CellStyle问题
    c# 字符串的比较大小
    c# 根据路径获取文件信息以及删除文件
    Eclipse 快捷键大全
    Smarty 的安装
    js实现页面跳转的几种方式
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10436018.html
Copyright © 2020-2023  润新知