• JDBC连接Hive数据库


    一、依赖

    pom

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jdk.version>1.8</jdk.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>2.1.1</version>
    </dependency>
    </dependencies>

    二、代码

    
    
    package com.qax.kylin.HiveUtil;

    import java.sql.*;

    /**
    * DESC:连接Hive数据库
    *
    * @author:wangshiheng
    * @date:2019/12/24
    */
    public class JDBCUtil {
    static final String DriverName="org.apache.hive.jdbc.HiveDriver";
    static final String url="jdbc:hive2://node06.research.com:10000";
    static final String user="";
    static final String pass="";

    /**
    * 创建连接
    * @return
    * @throws ClassNotFoundException
    * @throws SQLException
    */
    public static Connection getConn() throws ClassNotFoundException, SQLException {
    Class.forName(DriverName);
    Connection connection = DriverManager.getConnection(url,user,pass);
    return connection;
    }

    /**
    * 创建命令
    * @param connection
    * @return
    * @throws SQLException
    */
    public static Statement getStmt(Connection connection) throws SQLException {
    return connection.createStatement();
    }

    /**
    * 关闭连接
    * @param connection
    * @param statement
    * @throws SQLException
    */
    public void closeFunc(Connection connection,Statement statement) throws SQLException {
    statement.close();
    connection.close();
    }


    public static void main(String[] args) throws SQLException, ClassNotFoundException {

    Connection conn = JDBCUtil.getConn();
    Statement stmt = JDBCUtil.getStmt(conn);

    //执行sql语句
    String sql="select * from default.kylin_sales";
    ResultSet set = stmt.executeQuery(sql);//返回执行的结果集
    ResultSetMetaData meta = set.getMetaData();//获取字段
    while(set.next()) {
    for(int i=1;i<=meta.getColumnCount();i++) {
    System.out.print(set.getString(i)+" ");
    }
    System.out.println();
    }
    System.out.println("sql");
    }
    }
     

    三、执行结果

  • 相关阅读:
    webyestem(伊莱博)票据管理(ver1.0)数据库设计
    MicrosoftNorthwind(电子商务)数据库设计
    WebForm三层架构
    WebForm带接口工厂模式的三层架构
    VS 2008 C#代码调试进C++代码设置/远程调试
    C# 中使用指针
    互操作性——使用C/C++类型的非托管函数基础
    Perforce使用指南_forP4V
    [转]DotNet程序之找BUG心得
    C# 对XML基本操作总结
  • 原文地址:https://www.cnblogs.com/shwang/p/12091156.html
Copyright © 2020-2023  润新知