简体   繁体   中英

Connection to two different databases in single class

I need to connect to two different database from my code. First is Hive database and another one is Oracle. For this purpose I need to load two different drivers one for Hive and another for Oracle. Here is what I did:

        Class.forName("org.apache.hive.jdbc.HiveDriver");
        System.out.println("Driver Found");
        Connection connection = DriverManager.getConnection(
                "jdbc:hive2://10.8.219.36:10000/default", "lab", "lab");
        System.out.println("Connection established");

        System.out.println("------------------");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        System.out.println("Driver Found");
        Connection connection1 = DriverManager.getConnection(
                "jdbc:oracle:thin:@97.253.16.117:1521:NATTDB11",
                "CDR_LOADER", "CDR_LOADER");
        System.out.println("Connection established");

and when I am running the above code, this is what is got error:

Driver Found
Connection established
------------------
Driver Found
java.lang.IllegalArgumentException: Bad URL format
at org.apache.hive.jdbc.Utils.parseURL(Utils.java:185)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:84)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Demo.main(Demo.java:17)

How do I do this? Thank you in advance.

Try to explicitly register both drivers using:

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
DriverManager.registerDriver (new org.apache.hive.jdbc.HiveDriver());

Then test both Connection types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM