hive3.1.x:jdbc连接hive 作者:马育民 • 2021-03-18 10:37 • 阅读:10553 # 准备 ### 启动相关服务 [hive3.1.x 启动说明](https://www.malaoshi.top/show_1IX2HLjutY5g.html "hive3.1.x 启动说明") ### 准备数据 https://malaoshi.top/show_1IXlN51DbxF.html # 创建工程 创建maven工程,略 # 添加依赖 修改pom.xml ``` org.apache.hive hive-jdbc 3.1.2 org.apache.logging.log4j log4j-slf4j-impl ``` # java 可以使用 [DbUtil](https://www.malaoshi.top/show_1EF4t6cK0RGm.html "DbUtil") 类 ``` public static void main(String[] args) throws Exception { String driver="org.apache.hive.jdbc.HiveDriver"; String url="jdbc:hive2://hadoop1:10000/default"; String username="root"; String password=""; String sql="select empno,ename from emp"; //加载驱动类 Class.forName(driver); //创建连接 Connection conn = DriverManager.getConnection(url,username,password); //预编译 PreparedStatement ps = conn.prepareStatement(sql); //查询 ResultSet rs = ps.executeQuery(); //从rs中取出结果 while (rs.next()) { System.out.println(rs.getInt(1) + "," + rs.getString(2)); } rs.close(); ps.close(); conn.close(); } ``` 原文出处:http://malaoshi.top/show_1IXmUTeK8UE.html