查找已安装的JDBC驱动程序 [英] Finding Installed JDBC Drivers

查看:170
本文介绍了查找已安装的JDBC驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写数据库验证工具并具有首选项屏幕,因此用户可以定义他们的数据库连接。该工具至少应该能够处理DB2,Oracle,Postgresql和Mysql。

I'm writing a database validation tool in Java and have preference screens so the user can define their database connections. The tool should be able to cope with DB2, Oracle, Postgresql and Mysql as a minimum.

我真正想要的是能够向用户显示一个列表他们安装的jdbc驱动程序作为此过程的一部分。

What I would really like is to be able to present the user with a list of their installed jdbc drivers as part of this process.

任何人都可以提供代码片段来发现已安装的JDBC驱动程序吗?

Can anyone supply a code snippet for discovering installed JDBC drivers ?

推荐答案

为此,您需要扫描整个类路径(和子文件夹)以实现 java的类.sql.Driver 。通过这种方式,您还可以使用 Class#forName()手动加载的驱动程序,或者通过 META-INF自动加载/ services

To the point, you need to scan the entire classpath (and subfolders) for classes implementing java.sql.Driver. This way you will also cover drivers which are not loaded manually by Class#forName() or automagically by META-INF/services.

以下是一个基本示例:

public static void main(String[] args) throws Exception {
    List<Class<Driver>> drivers = findClassesImplementing(Driver.class);
    System.out.println(drivers);
}        

public static <T extends Object> List<Class<T>> findClassesImplementing(Class<T> cls) throws IOException {
    List<Class<T>> classes = new ArrayList<Class<T>>();

    for (URL root : Collections.list(Thread.currentThread().getContextClassLoader().getResources(""))) {
        for (File file : findFiles(new File(root.getFile()), ".+\\.jar$")) {
            JarFile jarFile = new JarFile(file);
            for (JarEntry jarEntry : Collections.list(jarFile.entries())) {
                String name = jarEntry.getName();
                if (name.endsWith(".class")) try {
                    Class<?> found = Class.forName(name.replace("/", ".").replaceAll("\\.class$", ""));
                    if (cls.isAssignableFrom(found)) {
                        classes.add((Class<T>) found);
                    }
                } catch (Throwable ignore) {
                    // No real class file, or JAR not in classpath, or missing links.
                }
            }
        }
    }

    return classes;
}

public static List<File> findFiles(File directory, final String pattern) throws IOException {
    File[] files = directory.listFiles(new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory() || file.getName().matches(pattern);
        }
    });

    List<File> found = new ArrayList<File>(files.length);

    for (File file : files) {
        if (file.isDirectory()) {
            found.addAll(findFiles(file, pattern));
        } else {
            found.add(file);
        }
    }

    return found;
}

相反你也可以考虑使用 Google Reflections API ,可以在一行中执行此操作:

Instead you can also consider to use the Google Reflections API which does this all in a single line:

Set<Class<? extends Driver>> drivers = reflections.getSubTypesOf(Driver.class);

这篇关于查找已安装的JDBC驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆