2016-04-30 31 views
1

我想用jsp來執行我的java代碼。該代碼包含配置單元連接和幾個簡單的查詢。當我剛剛運行java代碼時,它正在正確執行,但是當使用jsp執行時,它會在標題中顯示錯誤。java.sql.SQLException:找不到適合jdbc的驅動程序:hive2:// localhost:10000/default使用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> 
+3

你嘗試使用谷歌搜索的錯誤消息?這是最常見的錯誤之一,這個問題已經回答了數百次。 –

+0

也許您還沒有在您執行JSP的服務器上的WEB-INF \ lib目錄中部署相應的jar。 – RubioRic

+0

檢查你的路徑是否包含驅動庫lib –

回答

相關問題