java操作Apache druid

1. 添加maven依赖包

<dependency>
    <groupId>org.apache.calcite.avatica</groupId>
    <artifactId>avatica-core</artifactId>
    <version>1.15.0</version>
</dependency>

2. 编写工具类

package com.hnu.druid;

import org.apache.calcite.avatica.AvaticaConnection;
import org.apache.calcite.avatica.AvaticaStatement;
import org.springframework.stereotype.Component;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

/**
 * @description:
 * @author: YUANHX
 * @create: 7:11 下午
 **/
@Component
public class DruidJdbcUtil {

    private static ThreadLocal<AvaticaConnection> threadLocal = new ThreadLocal<>();

    private static final String DRUID_URL = "jdbc:avatica:remote:url=http://172.16.0.160:8888/druid/v2/sql/avatica/";

    /**
     * 打开连接
     * @param
     * @return
     * @throws SQLException
     */
    public static AvaticaConnection connection() throws SQLException {
        Properties properties = new Properties();
        AvaticaConnection connection = (AvaticaConnection) DriverManager.getConnection(DRUID_URL, properties);
        threadLocal.set(connection);
        return connection;
    }

    /**
     * 关闭连接
     * @throws SQLException
     */
    public static void closeConnection() throws SQLException{
        System.out.println("关闭线程:"+threadLocal.get());
        AvaticaConnection conn = threadLocal.get();
        if(conn != null){
            conn.close();
            threadLocal.remove();
        }
    }

    /**
     * 根据sql查询结果
     * @param
     * @param sql
     * @return
     * @throws SQLException
     */
    public static ResultSet executeQuery (String sql) throws SQLException{
        AvaticaStatement statement = connection().createStatement();
        ResultSet resultSet = statement.executeQuery(sql);

        return resultSet;

    }

    /*public static Object crud(String sql, Class clazz, List<Object> params) throws SQLException{
        AvaticaStatement statement = connection().createStatement();
        Object obj = null;
        for (int i = 0; i < params.size(); i++) {
            statement.set
        }
        return obj;
    }*/

    public static void main(String[] args) {
        try {
            String sql = "SELECT * FROM \"vehicleCondition\" limit 20";
            for (int i = 0; i < 5; i++) {
                ResultSet resultSet = executeQuery(sql);
                System.out.println("开始连接"+i + ";   连接线程:"+threadLocal.get());
                while(resultSet.next()){
                    String equipmentCode = resultSet.getString("EquipmentCode");
                    String vkaCode = resultSet.getString("VKACode");
//                    System.out.println(equipmentCode + "   ;   "+ vkaCode);
                }
                closeConnection();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一一可可

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值