JDBC驱动程序注册 JDBC简介(二)
2019-01-15 07:04:27来源:博客园 阅读 ()
驱动名称与URL格式
RDBMS | 驱动程序名称 |
URL格式
|
MySQL | com.mysql.jdbc.Driver |
jdbc:mysql://hostname/databaseName
|
ORACLE | oracle.jdbc.driver.OracleDriver |
jdbc:oracle:thin:@hostname:portNumber:databaseName
|
PostgreSQL | org.postgresql.Driver | jdbc:postgresql://hostname:port/dbname |
DB2 | com.ibm.db2.jdbc.net.DB2Driver | jdbc:db2:hostname:port Number/databaseName |
Sybase | com.sybase.jdbc.SybDriver | jdbc:sybase:Tds:hostname: portNumber/databaseName |
MYSQL的驱动下载
oracle驱动下载
驱动注册
package com.mysql.jdbc; import java.sql.SQLException; public class Driver extends NonRegisteringDriver implements java.sql.Driver { // // Register ourselves with the DriverManager // static { try { java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } } /** * Construct a new driver and register it with DriverManager * * @throws SQLException * if a database error occurs. */ public Driver() throws SQLException { // Required for Class.forName().newInstance() } }
- DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 依赖具体,没有面向抽象编程,不推荐
- System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver"); DriverManager加载前有用,不够灵活方便
- Class.forName("com.mysql.jdbc.Driver"); 通常使用
自动加载的4.0时代
private static void loadInitialDrivers() { String drivers; try { //获取环境变量中jdbc.drivers的列表 drivers = AccessController.doPrivileged(new PrivilegedAction<String>() { public String run() { return System.getProperty("jdbc.drivers"); } }); } catch (Exception ex) { drivers = null; } //如果按照规范在jar包中的META-INF/services设置了文件,将会加载为服务 // If the driver is packaged as a Service Provider, load it. // Get all the drivers through the classloader // exposed as a java.sql.Driver.class service. // ServiceLoader.load() replaces the sun.misc.Providers() AccessController.doPrivileged(new PrivilegedAction<Void>() { public Void run() { ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class); Iterator<Driver> driversIterator = loadedDrivers.iterator(); /* Load these drivers, so that they can be instantiated. * It may be the case that the driver class may not be there * i.e. there may be a packaged driver with the service class * as implementation of java.sql.Driver but the actual class * may be missing. In that case a java.util.ServiceConfigurationError * will be thrown at runtime by the VM trying to locate * and load the service. * * Adding a try catch block to catch those runtime errors * if driver not available in classpath but it's * packaged as service and that service is there in classpath. */ try{ //依次加载所有驱动,访问过后不就加载了嘛 while(driversIterator.hasNext()) { driversIterator.next(); } } catch(Throwable t) { // Do nothing } return null; } }); println("DriverManager.initialize: jdbc.drivers = " + drivers); //如果环境变量中没有设置的驱动程序,就可以结束了 //否则就将环境变量中的驱动程序加载一下 if (drivers == null || drivers.equals("")) { return; } String[] driversList = drivers.split(":"); println("number of Drivers:" + driversList.length); for (String aDriver : driversList) { try { println("DriverManager.Initialize: loading " + aDriver); Class.forName(aDriver, true, ClassLoader.getSystemClassLoader()); } catch (Exception ex) { println("DriverManager.Initialize: load failed: " + ex); } } }
- 读取系统属性jdbc.drivers,如果为空就拉到,如果存在将会进行加载。
- 检查jar包中的META-INF/services,如果有那么会自动注册,服务注册依赖的是ServiceLoader
总结
原文链接:https://www.cnblogs.com/noteless/p/10270107.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:数据库视图工具类
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- springboot~注册不同级别包里的bean~scanBasePackages 2020-06-10
- JSP+Structs+JDBC+mysql实现的诚欣电子商城 2020-06-08
- Spring11_JdbcTemplate 2020-06-07
- ahjesus-2020 Intellij idea 最新注册码 已稳定数月 亲测有 2020-05-25
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash