zoukankan      html  css  js  c++  java
  • ClickHouseUtil

    import com.alibaba.fastjson.JSONObject;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import javax.sql.DataSource;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    
    @Component
    @Slf4j
    public class ClickHouseUtil   {
    
        @Autowired
        private DataSource druidDataSource;
    
        public  List<JSONObject> exeSql(String sql){
            log.info("cliockhouse 执行sql:" + sql);
            Connection connection = null;
            try {
                connection = druidDataSource.getConnection();
            } catch (SQLException e) {
                log.error("获取数据库连接失败--{}",e.toString());
            }
            try {
                Statement statement = connection.createStatement();
                ResultSet results = statement.executeQuery(sql);
                ResultSetMetaData rsmd = results.getMetaData();
                List<JSONObject> list = new ArrayList();
                while(results.next()){
                    JSONObject row = new JSONObject();
                    for(int i = 1;i<=rsmd.getColumnCount();i++){
                        row.put(rsmd.getColumnName(i),results.getObject(rsmd.getColumnName(i)));
                    }
                    list.add(row);
                }
                return list;
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
  • 相关阅读:
    python 数据类型 变量 注释
    tornado 模版
    tornado 响应头 中断 状态码 工作流程
    tornado write render redirect IP
    tornado样板
    Celery实现异步任务
    Python pika简单实现RabbitMQ通信
    进程、线程与协程的比较
    使用 flask 实现 RESTful API
    阿里云服务器部署Tornado应用
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/15231972.html
Copyright © 2011-2022 走看看