java.sql.SQLException:找不到合适的jdbc驱动程序:hive2:// localhost:10000 / default使用jsp连接时

时间:2016-04-30 06:05:50

标签: java jsp jdbc hive

我正在尝试使用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>

1 个答案:

答案 0 :(得分:1)

您声明&#34; driverName&#34;,但从不使用它。

尝试添加:

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