java连接Hive

连接前准备

创建配置文件和日志信息文件,并将他们设置为资源包

在这里插入图片描述

在这里插入图片描述

一、BaseConfig类


import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class BaseConfig {
    private class Config{
        private String url;
        private String driver;
        private String username;
        private String password;
    }
    private  Config config;
//获取配置信息
    private void init() throws Exception {
        String path = Thread.currentThread().getContextClassLoader().
                getResource("datasource.properties").getPath();

        FileReader reader = new FileReader(path);
        Properties pro = new Properties();
        pro.load(reader);

        String url =pro.getProperty("url");
        if(null==url){
            throw new Exception("invalid url exception");
        }
        config=new Config();
        config.url =url;
        config.driver=pro.getProperty("driver","com.mysql.jdbc.Driver");
        config.username=pro.getProperty("username","root");
        config.password=pro.getProperty("password","");

    }


    {
        try {
            init();
            Class.forName(config.driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Connection getCon() throws SQLException {
        return DriverManager.getConnection(config.url,
                config.username,
                config.password);
    }
//关闭资源
    public void close(AutoCloseable...closeables){
        for (AutoCloseable closeable : closeables) {
            if(null != closeable){
                try {
                    closeable.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

二、BaseDao类


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BaseDao extends BaseConfig {
    private PreparedStatement getPst(Connection con, String sql, Object...params) throws SQLException {

        PreparedStatement pst = con.prepareStatement(sql);
        if (null != params) {
            for (int i = 0; i < params.length; i++) {
                pst.setObject(i + 1, params[i]);
            }
        }
        return pst;
    }

//执行增删改操作
    public Result exeNonQuery(String sql, Object...params) {
        Connection con = null;
        PreparedStatement pst = null;

        try {
            con = getCon();
            pst = getPst(con, sql, params);
            pst.execute();
            return Result.succed();
        } catch (SQLException e) {
            e.printStackTrace();
            return Result.fail();
        } finally {
            close(pst, con);
        }
    }
//执行查询操作
    public Result exeQuery(String sql, Object...params) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rst = null;

        try {
            con = getCon();
            pst = getPst(con, sql, params);
            rst = pst.executeQuery();
            List<List<String>> table = null;
            if (null != rst && rst.next()) {
                table = new ArrayList<>();
                final int COL = rst.getMetaData().getColumnCount();
                do {
                    List<String> row = new ArrayList<>(COL);
                    for (int i =1; i < COL; i++) {
                        row.add(rst.getObject(i).toString());
                    }
                    table.add(row);
                } while (rst.next());
            }
            return Result.succed(table);

        } catch (SQLException e) {
            e.printStackTrace();
            return Result.fail();
        } finally {
            close(rst, pst, con);
        }

    }

//读取sql语句
    public String readSql(String...paths) throws IOException {
        String path = paths.length == 0 ? "Sql/Sql.sql" : paths[0];

        StringBuilder builder = new StringBuilder();

        BufferedReader reader = new BufferedReader(new FileReader(path));

        String line = reader.readLine();
        if (null != line) {
            builder.append(line.trim() + " ");
        }
        return builder.toString();
    }

}


三、Result类


public abstract class Result<T> {
    private boolean err;
    private T data;

    public static <T> Result succed(T... d) {
        return new Result(false, d) {
        };

    }

    public static Result fail() {
        return new Result(true) {
        };
    }

    public Result(boolean err, T...data) {
        this.err = err;
        this.data = data.length > 0 ? data[0] : null;
    }

    public boolean isErr() {
        return err;
    }

    public T getData() {
        return data;
    }
}

四、测试结果类

package cn.kgc.hive.jdbc;

import cn.kgc.hive.jdbc.hdbc.BaseDao;
import cn.kgc.hive.jdbc.hdbc.Result;
import java.util.List;

public class App 
{
    public static void main( String[] args ) throws Exception {

        BaseDao dao = new BaseDao();
       Result<List<List<String>>> tables = dao.exeQuery(dao.readSql("D:\\KB10\\Codes\\hdbc\\Sql\\Sql.sql"));

       tables.getData().forEach(row->{
           row.forEach(cell->{
               System.out.print(cell+"\t");
           });
           System.out.println();
       });
     }
}

测试结果
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值