java操作Apache druid的实例代码
1.添加maven依赖包
org.apache.calcite.avatica avatica-core 1.15.0
2.编写工具类
packagecom.hnu.druid; importorg.apache.calcite.avatica.AvaticaConnection; importorg.apache.calcite.avatica.AvaticaStatement; importorg.springframework.stereotype.Component; importjava.sql.DriverManager; importjava.sql.ResultSet; importjava.sql.SQLException; importjava.util.List; importjava.util.Properties; /** *@description: *@author:YUANHX *@create:7:11下午 **/ @Component publicclassDruidJdbcUtil{ privatestaticThreadLocalthreadLocal=newThreadLocal<>(); privatestaticfinalStringDRUID_URL="jdbc:avatica:remote:url=http://172.16.0.160:8888/druid/v2/sql/avatica/"; /** *打开连接 *@param *@return *@throwsSQLException */ publicstaticAvaticaConnectionconnection()throwsSQLException{ Propertiesproperties=newProperties(); AvaticaConnectionconnection=(AvaticaConnection)DriverManager.getConnection(DRUID_URL,properties); threadLocal.set(connection); returnconnection; } /** *关闭连接 *@throwsSQLException */ publicstaticvoidcloseConnection()throwsSQLException{ System.out.println("关闭线程:"+threadLocal.get()); AvaticaConnectionconn=threadLocal.get(); if(conn!=null){ conn.close(); threadLocal.remove(); } } /** *根据sql查询结果 *@param *@paramsql *@return *@throwsSQLException */ publicstaticResultSetexecuteQuery(Stringsql)throwsSQLException{ AvaticaStatementstatement=connection().createStatement(); ResultSetresultSet=statement.executeQuery(sql); returnresultSet; } /*publicstaticObjectcrud(Stringsql,Classclazz,List