Java – JDBC driver not found when attempting to create a connection

JDBC driver not found when attempting to create a connection… here is a solution to the problem.

JDBC driver not found when attempting to create a connection

The Hive JDBC code throws an exception. I tried Hive 0.13.0, Hive 1.12.1, and Hive 0.12.0.

However, none of these create a connection.

package com.cisco.installbase.hiveconnector;

import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import java.sql.Connection;

public class CreateConnection {

private static final Logger LOGGER = Logger.getLogger(CreateConnection.class);

private static Connection instance = null;
    static final String drivername = "org.apache.hive.jdbc.HiveDriver";

private CreateConnection() {

try {
            LOGGER.info("Creating the connection");

Class.forName(drivername);
            instance = DriverManager.getConnection("jdbc:hive://hddev-c01-edge-02:9083/");
        } catch (ClassNotFoundException e) {
            LOGGER.error("Error occurred to create connection",e);
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
    }

public static Connection getInstance() {

LOGGER.info("Connection Instance");
        if (instance == null) {
            instance = (Connection) new CreateConnection();
        }
        return instance;
    }
}

Exception stack trace:

16/02/11 07:01:46 INFO hiveconnector. CreateConnection: Connection
Instance 16/02/11 07:01:46 INFO hiveconnector. CreateConnection:
Creating the connection 16/02/11 07:01:46 ERROR
hiveconnector. CreateConnection: No suitable driver found for
jdbc:hive://hddev-c01-edge-02:9083/ Exception in thread “main”
java.lang.ExceptionInInitializerError Caused by:
java.lang.ClassCastException: com.cisco.installbase.hiveconnector.Cre
ateConnection cannot be cast to java.sql.Connection at
com.cisco.installbase.hiveconnector.CreateConnection.getInstance(Crea
teConnection.java:39)
at com.cisco.installbase.hiveconnector.CommonDBUtilities. (CommonDBUtilities.java:19)
at com.cisco.installbase.hiveconnector.MainApp. (MainApp.java:33)

pom.xml

<dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>0.12.0</version>
        </dependency>
        < dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> 
            <version>1.2.1</version> </dependency>
        <dependency>

Solution

You have two problems, the first “No suitable driver found for jdbc:hive://hddev-c01-edge-02:9083/” means you have the wrong driver or the wrong URL of the driver you loaded.

When loading org.apache.hive.jdbc.HiveDriver , you are using , which uses jdbc:hive2://<host>::<port> as the connection string (note the 2 in the url).

The second question, please see the exception:

Exception in thread “main” java.lang.ExceptionInInitializerError Caused by: java.lang.ClassCastException: com.cisco.installbase.hiveconnector.CreateConnection cannot be cast to java.sql.Connection at com.cisco.installbase.hiveconnector.CreateConnection.getInstance(Crea teConnection.java:39)

And your code:

public static Connection getInstance() {
    LOGGER.info("Connection Instance");
    if (instance == null) {
        instance = (Connection) new CreateConnection();
    }
    return instance;
}

You are trying to convert one of your own instances of the CreateConnection class to java.sql.Connection, which is not the case. For now, your code doesn’t make much sense: you’re 1) initializing static fields in the constructor, and 2) trying to return said class as java.sql.Connection. As a side note: static, singleton connections to databases are generally not a good idea.

You can fix your code by changing the constructor to return a Connection method, but this will still give you the bad idea of having a singleton connection object:

public class CreateConnection {

private static final Logger LOGGER = Logger.getLogger(CreateConnection.class);

private static Connection instance = null;
    static final String drivername = "org.apache.hive.jdbc.HiveDriver";

public static Connection getInstance() {
        LOGGER.info("Connection Instance");
        if (instance == null) {
            instance = createConnection();
        }
        return instance;
    }

private static Connection createConnection() {
        try {
            LOGGER.info("Creating the connection");

Class.forName(drivername);
            return DriverManager.getConnection("jdbc:hive2://hddev-c01-edge-02:9083/");
        } catch (ClassNotFoundException e) {
            LOGGER.error("Error occurred to create connection",e);
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
    }
}