久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

java操作Apache druid的實例代碼

瀏覽:50日期: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; } /** * 關閉連接 * @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(); } }}

到此這篇關于java操作Apache druid的實例代碼的文章就介紹到這了,更多相關java操作Apache druid內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 汉阴县| 康马县| 宁国市| 乌海市| 连平县| 崇左市| 鲁山县| 汕头市| 淳安县| 镶黄旗| 甘泉县| 卓尼县| 泌阳县| 长白| 潮安县| 涿鹿县| 嘉禾县| 枣强县| 蕲春县| 屏东县| 颍上县| 德保县| 洪洞县| 偃师市| 合作市| 奇台县| 中西区| 东方市| 云阳县| 洱源县| 北京市| 宁城县| 富平县| 广宗县| 策勒县| 民丰县| 安远县| 塔河县| 东光县| 中江县| 乌审旗|