java操作Apache druid的實(shí)例代碼
更新時(shí)間:2020年11月13日 10:29:30 作者:一一可可
這篇文章主要介紹了java操作Apache druid的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
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;
}
/**
* 關(guān)閉連接
* @throws SQLException
*/
public static void closeConnection() throws SQLException{
System.out.println("關(guān)閉線程:"+threadLocal.get());
AvaticaConnection conn = threadLocal.get();
if(conn != null){
conn.close();
threadLocal.remove();
}
}
/**
* 根據(jù)sql查詢結(jié)果
* @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();
}
}
}
到此這篇關(guān)于java操作Apache druid的實(shí)例代碼的文章就介紹到這了,更多相關(guān)java操作Apache druid內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Apache Calcite進(jìn)行SQL解析(java代碼實(shí)例)
- Java使用Apache.POI中HSSFWorkbook導(dǎo)出到Excel的實(shí)現(xiàn)方法
- 淺析Java中Apache BeanUtils和Spring BeanUtils的用法
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer異常
- Java常用類庫Apache Commons工具類說明及使用實(shí)例詳解
- Java利用apache ftp工具實(shí)現(xiàn)文件上傳下載和刪除功能
- Java?Apache?common-pool對象池介紹
相關(guān)文章
SpringBoot如何讀取mock數(shù)據(jù)(高效調(diào)試接口)
本文介紹如何在SpringBoot項(xiàng)目中讀取resources目錄下的mock數(shù)據(jù)文件,以便高效調(diào)試接口,在 Spring Boot 項(xiàng)目中,通常會將靜態(tài)資源或配置文件放在 src/main/resources 目錄下,下面通過實(shí)例給大家詳細(xì)介紹,感興趣的朋友一起看看吧2024-12-12
詳解SpringBoot中的統(tǒng)一功能處理的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)統(tǒng)一功能處理,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定借鑒價(jià)值,需要的可以參考一下2023-01-01
xxl-job 帶參數(shù)執(zhí)行和高可用部署方法
這篇文章主要介紹了xxl-job 帶參數(shù)執(zhí)行和高可用部署,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
詳解Spring Cloud 跨服務(wù)數(shù)據(jù)聚合框架
這篇文章主要介紹了詳解Spring Cloud 跨服務(wù)數(shù)據(jù)聚合框架,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Java常用面板之JScrollPane滾動面板實(shí)例詳解
這篇文章主要介紹了Java常用面板JScrollPane的簡單介紹和一個(gè)相關(guān)實(shí)例,,需要的朋友可以參考下。2017-08-08

