多平台统一管理软件接口,如何实现多平台统一管理软件接口
308
2022-11-13
java操作Apache druid的实例代码
1. 添加maven依赖包
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
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;
}
/*pubhttp://lic static Object crud(String sql, Class clazz, Lishttp://t
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();
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~