gpt4 book ai didi

java - 尝试创建连接时未找到 JDBC 驱动程序

转载 作者:可可西里 更新时间:2023-11-01 15:02:13 26 4
gpt4 key购买 nike

Hive JDBC 代码抛出异常。我尝试使用 Hive 0.13.0、Hive 1.12.1 和 Hive 0.12.0。

但是,这些都没有创建连接。

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

异常堆栈跟踪:

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>

最佳答案

你有两个问题,第一个“No suitable driver found for jdbc:hive://hddev-c01-edge-02:9083/” 意味着你有错误的驱动程序或您加载的驱动程序的 URL 错误。

加载时 org.apache.hive.jdbc.HiveDriver ,您正在使用 HiveServer2 client ,它使用 jdbc:hive2://<host>:<port>作为连接字符串(注意 url 中的 2)。

第二个问题请看异常:

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)

和你的代码:

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

您正在尝试转换您自己的一个实例 CreateConnection类到 java.sql.Connection ,事实并非如此。就目前而言,您的代码没有多大意义:您是 1) 在构造函数中初始化静态字段,以及 2) 试图将所述类作为 java.sql.Connection 返回.附带说明:与数据库的静态、单例连接通常不是一个好主意。

您可以通过将构造函数更改为返回 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());
}
}
}

关于java - 尝试创建连接时未找到 JDBC 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com