3.获取数据库连接方式

文章目录

1.方式一(了解)

1)数据库版本8.0.25

import org.junit.Test;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {
    @Test
    public void connection() throws SQLException {

        Driver driver =new com.mysql.cj.jdbc.Driver(); //ctrl + h查看Driver接口的实现类

        //url:http://localhost:8080/gmall/keyboard.jpg
        //jdbc:mysql:协议
        //localhost:ip:地址
        //3306:默认mysql的端口号
        //test:test数据库
        String url = "jdbc:mysql://localhost:3306/test"; 
        //将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user","root");//数据库用户名
        info.setProperty("password","123456");//数据库密码

        Connection conn = driver.connect(url,info);
        System.out.println(conn);
    }
}

2)"jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8";
表示获取时区,为东八区,执行完上面代码出错可以试试添加时区,再出错就先打开数据库,再执行上面代码。

2.方式二(了解)

1)IDEA版本:1.8

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

    //2.提供要连接的数据库
    String url = "jdbc:mysql://localhost:3306/test";
    //3.提供连接需要的用户名和密码
    Properties info = new Properties();
    info.setProperty("user","root");
    info.setProperty("password","123456");

    //4.获取连接
    Connection conn = driver.connect(url,info);
    System.out.println(conn);
}

2)使用了反射,未使用第三方API,提高了可移植性。

3.方式三(了解)

1)使用DriverManager替代Driver

@Test
    public void connection2() throws Exception {
        //1.获取Driver实现类的对象
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver)clazz.getDeclaredConstructor().newInstance();

        //2.提供另外个连接的基本信息
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";

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

        //4.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
    }

4.方式四(了解)

1)对方式三的优化:

//方式四:对方式三优化
    @Test
    public void connection3() throws Exception {
        //1.提供另外个连接的基本信息
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";

        //2.加载Driver
        Class.forName("com.mysql.cj.jdbc.Driver");

        //4.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
    }

2)相较方式三来说:在mysql的Driver实现类中,声明了如下操作

ublic class Driver extends NonRegisteringDriver implements java.sql.Driver {
    public Driver() throws SQLException {
    }

    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
}

方式五:最终版

1)在要操作的Moudule下创建file文件,名字为jdbc.properties

user=root
password=123456
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.cj.jdbc.Driver
@Test
    public void connection4() throws Exception {
        //1.读取配置文件中的四个操作
        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");

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

        //3.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
    }

2)好处:实现了代码与数据分离。

上一篇:ResultMap结果集映射


下一篇:一起学习PHP中的FTP操作