1

I am trying to execute my java code using jsp. The code contains hive connection and few simple queries. When I just run the java code it is executing properly, but when executing using jsp it shows the error in the title.

java code

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 code

<%@ 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>
Ajinkya
  • 201
  • 4
  • 11
  • 3
    Did you try using Google to search for the error message? This is one of the most common errors and the question has been answered hundreds of times. – Jim Garrison Apr 30 '16 at 06:07
  • Maybe you haven't deployed the corresponding jar in your WEB-INF\lib directory on the server that you're executing your JSP. – RubioRic Apr 30 '16 at 06:09
  • check your path contains the driver lib – KP. Apr 30 '16 at 06:10
  • I already tried that, but its showing same error @RubioRic – Ajinkya Apr 30 '16 at 06:41
  • @JimGarrison I am not getting the error while running normal java code which is a common problem, but running using a jsp is showing errors – Ajinkya Apr 30 '16 at 06:45
  • http://stackoverflow.com/questions/33722139/java-sql-sqlexception-no-suitable-driver-found-for-jdbchive-localhost10000 May be is this the problem?? – ranafeb14 Apr 30 '16 at 06:51
  • 1
    @Ajinkya if it works standalone and not in the Servlet container, then you have not included the required jar file(s) in your deployment so they can be seen by the servlet engine. – Jim Garrison Apr 30 '16 at 06:56
  • You can try Class.forName(jdbcDriver); before obtaining the connection. – dimplex Apr 30 '16 at 07:29
  • Possible duplicate of [java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver](http://stackoverflow.com/questions/5616898/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmicrosoftsqlserver) – Dennis Jaheruddin May 03 '16 at 12:04

1 Answers1

1

You declare "driverName", but never use it.

Try adding:

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

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

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124