没有Class.forName()或System Properties的JDBC驱动程序如何加载?

时间:2019-01-10 04:10:52

标签: java jdbc properties system

我试图了解JDBC内部,特别是,如果出现以下情况,如何加载我的JDBC驱动程序: 1.我没有使用Class.forName() 2.检查jdbc.driver系统属性是否返回null。

我尝试检查类路径并将系统属性的完整列表打印到控制台,以按照https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html#registerDriver-java.sql.Driver-https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html

进行检查

下面是DBConnection类中的DBConnection构造函数

    `public DBConnection() {
        try {
            this.conn = DriverManager.getConnection(JDBC_URL);
            if (this.conn != null) {
                System.out.println("Connection successful");
            }
        } catch(SQLException sqlex) {
            System.out.println("Connection failed");
        }
    }`
下面是main()

    public static void main(String[] args) {

        DBConnection dbTest = new DBConnection();

        String sysPropsString = System.getProperties().toString();
        String[] propsArr = sysPropsString.split(",");

        for(String property : propsArr) {
            if (property.contains("class") && property.contains("path") 
                    && (property.contains("derby") || property.contains("drivers")))
                System.out.println(property);
        }
        System.out.println("***********************************");
        String sysDrivers = System.getProperty("jdbc.drivers");
        System.out.println(sysDrivers);
    }


我希望从System.getProperty()调用OR将derby jdbc驱动程序打印到控制台,或者在类路径的某个位置找到它,但是我什么都看不到。 derby驱动程序如何加载?

下面是输出:

  

连接成功

     
    

java.class.path = /用户/aslotu/eclipse-workspace/Bullhorn/build/classes:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_cs.jar: /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_de_DE.jar:/Library/Java/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_es。 jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_fr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/ derbyLocale_hu.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_it.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/ lib / derbyLocale_ja_JP.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_ko_KR.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/ db / lib / derbyLocale_pl.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_pt_BR.jar:/ Lib rary / Java / JavaVirtualMachines / jdk1.8.0_121.jdk / Contents / Home / db / lib / derbyLocale_ru.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_zh_CN.jar :/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derbyLocale_zh_TW.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/db/lib/derby .jar

         
         

  

1 个答案:

答案 0 :(得分:0)

derby.jar包含文件META-INF/services/java.sql.Driver,该文件注册org.apache.derby.jdbc.AutoloadedDriver

这项工作是因为DriverManager使用ServiceLoader