Handle Multiple JDBC Drivers from ORDER

Yesterday I ran into a big problem. In my current project, I am using ojdbc6 an implementation of Oracle JDBC to connect, but I also need to process, for example, the oracle of 8 databases, which is completely impossible with this JAR. You would say that I should use ojdbc14, for example, which was right for some tests, but suggests that later I will need to process 2 types of databases from the same provider, but we know that for BOTH , and I these must be loaded at the same time . The same interface (and well, not only the same interface, the same class structure, just a different implementation inside!), The same URL connection prefix β†’ JDBC connection will use the same driver, but I cannot load several of them. So what now?

  • My first idea was to load the JAR with different class loaders, maybe I could load the same package structure with the same classes that were separated from each other? Actually, I don’t think so, maybe it was a stupid idea. This can also be a common problem later not only with JDBC drivers, so even if you cannot answer my question, but know what is missing here, tell me

  • Even if I could separately load class implementations from the same class names, how can I tell DriverManager to create a connection to use the EXACT driver instead of finding it based on the connection url prefix? (where I mean jdbc: oracle: thin, for example).

Now I feel completely dumb, because I think this is not an unusual idea to work in the Java world, BUT I don’t completely know how to handle it.

Thanks for y'all in advance

+5
source share
2 answers

You have several options:

  • You can try loading drivers from different class loaders. This will work if you only need pure JDBC in your application. I doubt that you will get Hibernate to work with such a setting.

    In the end, you will need to run the code where you will need to see instances from both class loaders, and here you will get ClassCastException(two classes with the same full name are different when loading from different class loaders).

  • . - , JDBC . Oracle 8, .

    , .

  • Oracle 8 , CREATE DATABASE LINK. , . , Oracle .

    , Oracle 8 , , .

  • JDBC Oracle , . " JAR", ? Oracle 10 Oracle 7 . , .

+5
#jdbc.properties
oracle.driver=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@//localhost/xe
oracle.user=scott
oracle.password=tiger

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost/sales
mysql.user=root

mssql.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
mssql.url=jdbc:sqlserver://192.168.1.175;databaseName=sales
mssql.user=dbviewer
mssql.password=dbviewer

:

class QueryTest2 {

   public static void main(String[] args) throws Exception{
        Properties settings = new Properties();
        FileInputStream fin = new FileInputStream("jdbc.properties");
        settings.load(fin);
        fin.close();
        String dvr = settings.getProperty(args[0] + ".driver");
        String url = settings.getProperty(args[0] + ".url");
        String usr = settings.getProperty(args[0] + ".user");
        String pwd = settings.getProperty(args[0] + ".password");
        Class.forName(dvr);
        Connection con = DriverManager.getConnection(url, usr, pwd);
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select pno,price,stock from products");
        while(rs.next()){
            System.out.printf("%d\t%.2f\t%d%n", rs.getInt(1), rs.getDouble(2), rs.getInt("stock"));
        }
        rs.close();
        stmt.close();
        con.close();
    }
}
-1

All Articles