ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

3.获取数据库连接方式

2022-01-20 00:01:32  阅读:92  来源: 互联网

标签:jdbc String url 数据库 Driver 获取 mysql password 连接


文章目录

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)好处:实现了代码与数据分离。

标签:jdbc,String,url,数据库,Driver,获取,mysql,password,连接
来源: https://blog.csdn.net/m0_59376721/article/details/122591339

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有