国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java操作Apache druid的實(shí)例代碼

瀏覽:43日期:2022-08-21 10:31:46

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 下午 **/@Componentpublic 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)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 赣州市| 遂溪县| 彝良县| 梧州市| 买车| 新郑市| 双牌县| 德令哈市| 睢宁县| 白玉县| 锡林郭勒盟| 平谷区| 包头市| 德钦县| 自治县| 墨竹工卡县| 克山县| 长武县| 自贡市| 汪清县| 洛川县| 泽州县| 黑河市| 白玉县| 铜川市| 克东县| 高青县| 邵东县| 罗定市| 黄平县| 尼勒克县| 麻阳| 太谷县| 边坝县| 建平县| 启东市| 化德县| 富宁县| 浠水县| 仁布县| 太谷县|