启动远程客户端 # hive --service hiveserver2
获取连接-〉创建运行环境-〉执行HQL-〉处理结果-〉释放资源
工具类
1 package demo.utils; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 10 public class JDBCUtils { 11 private static String driverString = "org.apache.hive.jdbc.HiveDriver"; 12 private static String urlString = "jdbc:hive2://sd-9c1f-2eac:10000/default"; 13 static { 14 try { 15 Class.forName(driverString); 16 } catch (ClassNotFoundException e) { 17 throw new ExceptionInInitializerError(e); 18 } 19 } 20 21 22 public static Connection getConnection() { 23 try { 24 return DriverManager.getConnection(urlString); 25 } catch (SQLException e) { 26 e.printStackTrace(); 27 } 28 return null; 29 } 30 31 public static void release (Connection conn, Statement st, ResultSet rs){ 32 if (rs!=null){ 33 try{ 34 rs.close(); 35 }catch(SQLException e){ 36 e.printStackTrace(); 37 }finally{ 38 rs=null; 39 } 40 } 41 if (st!=null){ 42 try{ 43 st.close(); 44 }catch(SQLException e){ 45 e.printStackTrace(); 46 }finally{ 47 st=null; 48 } 49 } 50 if (conn!=null){ 51 try{ 52 conn.close(); 53 }catch(SQLException e){ 54 e.printStackTrace(); 55 }finally{ 56 conn=null; 57 } 58 } 59 } 60 61 }