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不支持该操作
返回此驱动程序使用的所有日志记录器的父记录器