后端开发JDBC获取数据库连接的方式

Posted cls1277

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后端开发JDBC获取数据库连接的方式相关的知识,希望对你有一定的参考价值。

一共有六种方式,但是不是并列关系,而是迭代关系

最后一种方式最好,新建一个jdbc.properties配置文件,通过最后一种方式读取配置文件连接数据库

找到mysql版本对应的connection的包就可以不用听别人的

https://mvnrepository.com/artifact/mysql/mysql-connector-java

通过看源代码可以发现现在的包名加个了.cj,其实区别还是不大

下面的代码还是很好理解的除了已经被我忘掉的反射,记得复习一下

不要想了,下面的密码都是我修改过的了

package com.cls1277.connection;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

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

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

    @Test
    public void connectionTest3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException 
        Class clas = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clas.newInstance();
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "xxx";
        DriverManager.registerDriver(driver);
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    

    @Test
    public void connectionTest4() throws ClassNotFoundException, SQLException 
        //mysql提供的驱动的类中有静态代码块,在加载类的时候执行,因此直接new了一个新的对象
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "xxx";
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    

    @Test
    public void connectionTest5() throws SQLException 
        //最好第4种,那句话不要省,只是mysql可以省而已
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "xxx";
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    

    @Test
    public void connectTest6() throws IOException, ClassNotFoundException, SQLException 
        InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(resourceAsStream);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverclass = pros.getProperty("driverClass");
        Class.forName(driverclass);
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    


以上是关于后端开发JDBC获取数据库连接的方式的主要内容,如果未能解决你的问题,请参考以下文章

快速学会JDBC及获取连接的五种方式

JDBC—— 获取连接池方式

JDBC

JDBC 数据库的几种连接方式

JDBC 数据库的几种连接方式

JDBC 数据库的几种连接方式