Java(113):Java通过jdbc连接hive3.12

Java(113):Java通过jdbc连接hive

maven引用:

<dependency>
    <groupId>org.appache.hive</groupId>
    <artifactId>hive-jdbc-uber</artifactId>
    <version>2.6.5.0-292</version>
    <scope>system</scope>
    <systemPath>${pom.basedir}/src/main/resources/lib/hive-jdbc-uber-2.6.5.0-292.jar</systemPath>
</dependency>

Java通过jdbc连接hive的调试类:

dbUtil.java

//package utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Description :
 *
 * @author : HMF
 * Date : Created in 15:28 2023/3/21
 * @version :
 */
public class dbUtil {
    public Connection conn;
    public dbUtil(){
		String driver="org.apache.hive.jdbc.HiveDriver";
        String url="jdbc:hive2://10.1.1.242:10000/regre_one?useUnicode=true&characterEncoding=UTF-8";
        String user="xxxxxx";
        String password="xxxxxx";

        try{
            Class.forName(driver);
            conn= DriverManager.getConnection(url,user,password);
        }catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }

    }
    public static void main(String[] args)  {
        dbUtil db=new dbUtil();
        String insertSql="INSERT INTO regre_one.hivetest_varchar (id,aes, sm4, sm4_a, email, phone, ssn, military, passport, intelssn, intelpassport, intelmilitary, intelganghui, inteltaitonei, credit_card_short, credit_card_long, job) VALUES (1,'小芬', '北京xx网络技术有限公司', '北京市', 'hmf@yahoo.com', '15652996964', '210302199608124861', '武水电字第3632734号', 'BWP018930705', '210302199608124861', 'BWP018930705', '武水电字第3632734号', 'H21157232', '9839487602', '117', '6227612145830440', '高级测试开发工程师')";
        String selectSql="select id,aes, sm4, sm4_a, email, phone, ssn, military, passport, intelssn, intelpassport, intelmilitary, intelganghui, inteltaitonei, credit_card_short, credit_card_long, job from regre_one.hivetest_varchar";
        String deleteSql="truncate table regre_one.hivetest_varchar";
        db.DBExecute(deleteSql);
        db.DBExecute(insertSql);
        db.DBQuery(selectSql);
        System.exit(0);
    }

    void DBExecute(String sqlStr){
        try{
            Statement stmt=conn.createStatement();
            boolean result=stmt.execute(sqlStr);
            System.out.println("+++++sqlStr:"+sqlStr);
            stmt.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    void DBQuery(String sqlStr){
        try{
             Statement statement=conn.createStatement();
             ResultSet rs=statement.executeQuery(sqlStr);
             int columnCount=rs.getMetaData().getColumnCount();
             System.out.println("+++++sqlStr:"+sqlStr);
             while (rs.next()){
                 String result="";
                 for(int i=0;i<columnCount;i++){
                     result +="\t"+rs.getString(i+1);
                 }
                 System.out.println(result);
             }
             rs.close();
             statement.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值