简体   繁体   English

尝试创建连接时找不到JDBC驱动程序

[英]JDBC Driver not found while try to create a connection

Hive JDBC Code throws exception.I tried using Hive 0.13.0,Hive 1.12.1 and Hive 0.12.0. Hive JDBC代码引发异常。我尝试使用Hive 0.13.0,Hive 1.12.1和Hive 0.12.0。

But, none of these are creating 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 StackTrace: 异常StackTrace:

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) 16/02/11 07:01:46 INFO hiveconnector.CreateConnection:连接实例16/02/11 07:01:46 INFO hiveconnector.CreateConnection:创建连接16/02/11 07:01:46错误hiveconnector.CreateConnection:找不到适用于jdbc的驱动程序:hive:// hddev-c01-edge-02:9083 /线程“主”中的异常java.lang.ExceptionInInitializerError原因:java.lang.ClassCastException:com.cisco.installbase.hiveconnector.Cre ateConnection无法在com.cisco.installbase.hiveconnector.CommonDBUtilities。(CommonDBUtilities.java:19)的com.cisco.installbase.hiveconnector.CreateConnection.getInstance(Crea teConnection.java:39)处转换为java.sql.Connection .cisco.installbase.hiveconnector.MainApp。(MainApp.java:33)

pom.xml 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>

You have two problems, the first "No suitable driver found for jdbc:hive://hddev-c01-edge-02:9083/" means that you either have the wrong driver or the wrong URL for the driver that you loaded. 您有两个问题,第一个是“找不到适用于jdbc:hive:// hddev-c01-edge-02:9083 /的合适的驱动程序”,这意味着您使用的驱动程序错误或URL错误。

As you load org.apache.hive.jdbc.HiveDriver , you are using the HiveServer2 client , which uses jdbc:hive2://<host>:<port> as the connection string (note the 2 in the url). 加载org.apache.hive.jdbc.HiveDriver ,您正在使用HiveServer2客户端 ,该客户端使用jdbc:hive2://<host>:<port>作为连接字符串(请注意url中的2 )。

For the second problem, please look at 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) 线程“主”中的异常java.lang.ExceptionInInitializerError原因:java.lang.ClassCastException: com.cisco.installbase.hiveconnector.CreateConnection无法转换为com.cisco.installbase.hiveconnector.CreateConnection.getInstance上的java.sql.Connection 。 (创建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 cast an instance of your own CreateConnection class to a java.sql.Connection , which it isn't. 您试图将自己的CreateConnection类的实例CreateConnection转换为java.sql.Connection ,但并非如此。 As it stands your code doesn't make a lot of sense: you are 1) initializing a static field in a constructor, and 2) trying to return said class as a java.sql.Connection . 就目前而言,您的代码没有什么意义:您是1)在构造函数中初始化静态字段,并且2)尝试将上述类返回为java.sql.Connection As a side note: static, singleton connections to a database are generally a bad idea. 附带说明:与数据库的静态单例连接通常不是一个好主意。

You could fix your code by changing the constructor to a method that returns a Connection , but this will still leave you with the bad idea of having a singleton connection object: 您可以通过将构造函数更改为返回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";

    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());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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