【问题标题】:java.sql.SQLException: No suitable driver found for jdbc:hive2://localhost:10000/default while connecting using jspjava.sql.SQLException: 使用 jsp 连接时找不到适合 jdbc:hive2://localhost:10000/default 的驱动程序
【发布时间】:2016-08-25 09:17:04
【问题描述】:

我正在尝试使用 jsp 执行我的 java 代码。该代码包含配置单元连接和一些简单的查询。当我只是运行java代码时,它正在正确执行,但是当使用jsp执行时,它会在标题中显示错误。

java代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import org.apache.hive.jdbc.HiveDriver;
public class Hive {
public void hive1() throws SQLException {

try{

String driverName = "org.apache.hive.jdbc.HiveDriver";

Connection conn = DriverManager.getConnection(
"jdbc:hive2://localhost:10000/default", "hduser", "abc");

System.out.println("Connected");
Statement stmt = conn.createStatement();

stmt.execute("CREATE TABLE IF NOT EXISTS "
+" tweets5 ( id BIGINT, created_at STRING, "
+"source STRING, "
+"favorited BOOLEAN, "
+" retweet_count INT,"
+"retweeted_status STRUCT< "
+"text:STRING, "
+" user:STRUCT<screen_name:STRING,name:STRING>,"
+"retweet_count:INT>, "
+" entities STRUCT< "
+"urls:ARRAY<STRUCT<expanded_url:STRING>>, "
+"user_mentions:ARRAY<STRUCT<screen_name:STRING,name:STRING>>, "
+" hashtags:ARRAY<STRUCT<text:STRING>>>,"
+" text STRING,"
+" user STRUCT<"
+"screen_name:STRING, "
+"name:STRING, "
+"locations:STRING, "
+"friends_count:INT, " 
+" followers_count:INT,"
+"statuses_count:INT, "
+"verified:BOOLEAN, "
+"utc_offset:INT, "
+"time_zone:STRING>, "
+ " in_reply_to_screen_name STRING)"
+" ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'"
+" LOCATION '/user/flume/tweets'");
System.out.println("Table created successfully");


String sql = "describe tweets5";
ResultSet res = stmt.executeQuery(sql);
while (res.next()) 
{      
    System.out.println(res.getString(1) + "\t" + res.getString(2));
}



sql = "select id,user.name from tweets5";
System.out.println("\nRunning: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getString(1)) + "\t" +     String.valueOf(res.getString(2)));
}
}
System.out.println("Operation done successfully.");
stmt.close();
conn.close();

System.out.println("End");
}catch(SQLException se){
se.printStackTrace();
}

catch(Exception e){
e.printStackTrace();
}
}
/*
public static void main(String[] args) throws SQLException {

Hive h = new Hive();
h.hive1();
}
*/
}

jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%>

<%@ page import="hive.Hive" %>

<%@ page import="java.util.*"%>

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.*"%>
<%@ page import="org.apache.hive.jdbc.HiveDriver" %>

<!DOCTYPE html>
<html>
<head>

    <title>page4</title>
</head>
<body>


   <% 

   Hive h = new Hive();
   h.hive1();

   %>

   <%= "<h1> Processing started....</h1>" %>


</body>
</html>

【问题讨论】:

  • 您是否尝试使用 Google 搜索错误消息?这是最常见的错误之一,这个问题已经回答了数百次。
  • 可能你在执行 JSP 的服务器的 WEB-INF\lib 目录下没有部署对应的 jar。
  • 检查您的路径是否包含驱动程序库
  • 我已经尝试过了,但它显示相同的错误@RubioRic
  • @Ajinkya 如果它独立工作而不是在 Servlet 容器中,那么您没有在部署中包含所需的 jar 文件,以便 servlet 引擎可以看到它们。

标签: java jsp jdbc hive


【解决方案1】:

您声明了“driverName”,但从不使用它。

尝试添加:

try {
      Class.forName(driverName);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }

(来自https://community.hortonworks.com/articles/25410/simple-steps-to-test-hive-jdbc-connect.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2021-08-30
    • 2012-07-30
    • 2014-06-09
    • 2020-05-21
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多