java JDBC接口之Driver (1)

本文介绍了MySQL数据库驱动的Driver接口,包括`connect`方法用于建立连接、`acceptsURL`判断支持的协议、`getPropertyInfo`获取属性信息、版本号获取等。重点展示了如何通过Maven配置及代码实例来操作这些功能。
摘要由CSDN通过智能技术生成

1. Driver 接口

每个数据库驱动程序类必须实现的接口。 MySQL的是 com.mysql.jdbc.Driver

主要作用是注册数据源驱动,提供统一的接口

方法详情:

1. Connection connect​(String url, Properties info) throws SQLException

尝试与给定的URL建立数据库连接。 如果驱动程序意识到连接到给定URL的驱动程序类型错误,则应返回“null”。 这很常见,因为当要求JDBC驱动程序管理器连接到给定的URL时,它会依次将URL传递给每个加载的驱动程序。以mysql为例,首先添加mysql驱动包,

maven方式:

 <dependency>
        <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.29</version>
</dependency>

编写代码获取连接


import com.mysql.jdbc.Driver;

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

public class Main {
    private static final String username = "root";
    private static final String password = "root";
    private static final String url="jdbc:mysql://bar-mysql:3306/bar_baruser?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai";
    public static void main(String[] args) throws Exception {
        final Driver driver = new Driver();
        final Properties properties = new Properties();
        properties.put("user",username);
        properties.put("password",password);
        final Connection connect1 = driver.connect(url, properties);
        System.out.println(driver);

    }
}

输出

com.mysql.jdbc.Driver@7a69b07

2. boolean acceptsURL​(String url) throws SQLException

检索驱动程序是否认为它可以打开与给定URL的连接。用来检测该驱动是否支持该协议:

以mysql为例:

 public static void main(String[] args) throws Exception {
        final Driver driver = new Driver();
        final boolean acceptsURL = driver.acceptsURL("jdbc:mysql://");
        System.out.println(acceptsURL);
        // true
    }

如果是其他协议:则返回false:列

/**
 * @author zeng 
 */
public class Main {
    public static void main(String[] args) throws Exception {
        final Driver driver = new Driver();
        final boolean acceptsURL = driver.acceptsURL("jdbc:sqlite://");
        System.out.println(acceptsURL);
        // false
    }
}

3.DriverPropertyInfo[] getPropertyInfo​(String url, Properties info)

获取有关此驱动程序的可能属性的信息。列如:MySQL为例:

/**
 * @author zeng
 */

public class Main {
    private static final String username = "root";
    private static final String password = "root";
    private static final String url="jdbc:mysql://bar-mysql:3306/bar_baruser?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai";
    public static void main(String[] args) throws Exception {
        final Driver driver = new Driver();
        final Properties info = new Properties();
        info.setProperty("user",username);
        info.setProperty("password",password);
        final DriverPropertyInfo[] propertyInfo = driver.getPropertyInfo(url, info);
        Arrays.stream(propertyInfo).forEach(property->{
            System.out.println("描述信息:"+property.description);
            System.out.println("名称"+property.name);
            System.out.println("值"+property.value);
        });
        // 输出:连接的信息和数据库相关的信息,包括MySQL密码等等一系列信息,驱动信息,连接池等等
    }
}

3.int getMajorVersion()

这个驱动程序的主要版本号,我的是mysql8.0.29,所以输出8

/**
 * @author zeng
 */
public class Main {

    public static void main(String[] args) throws Exception {
        final Driver driver = new Driver();
        final int majorVersion = driver.getMajorVersion();
        System.out.println(majorVersion);
        // 8
    }
}

4.int getMinorVersion()

获取驱动的次版本号 mysql的驱动返回值和getMajorVersion一致

源代码

 @Override
    public int getMajorVersion() {
        return getMajorVersionInternal();
    }

5. boolean jdbcCompliant() 检查驱动是否标准的驱动,如果是则true,否则false

MySQL不符合SQL92标准,所以返回的是false,但是mysql符合jdbc标准,所以DatabaseMetaData.supportsANSI92EntryLevelSQL()返回的是true

6. public Logger getParentLogger()  mysql不支持该操作

返回此驱动程序使用的所有日志记录器的父记录器

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值