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; } }