/\*\*
* 连接对象批量关闭
* @param autoCloseable 连接对象集
*/
void close(AutoCloseable… autoCloseable);
/\*\*
* 基本操作SQL执行类
* @param con 连接对象
* @param sql sql命令
* @param params 参数
* @return 布尔
*/
boolean baseSqlExecute(Connection con, String sql, String…params);
boolean insert(Connection con, String sql, String…params);
boolean delete(Connection con, String sql, String…params);
/**
* 基本查询SQL执行类
* @param con 连接对象
* @param sql sql命令
* @param params 参数
* @return 数据集合
*/
List<Map<String, Object>> queryResultSet(Connection con, String sql, String…params);
}
clickhouse接口实现类:
import com.clickhouse.jdbc.ClickHousePreparedStatement;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.util.EnumUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class ClickHouseUtils implements Utils{
@Override
public Connection connection(ConnectionObj connectionObj) {
Connection con = null;
try {
// 获取驱动类
Class.forName(connectionObj.getDriverName());
// 建立连接获取连接对象
con = DriverManager.getConnection(connectionObj.getUrl(), connectionObj.getUser(), connectionObj.getPassword());
}catch (Exception e){
log.error(“Connection fail, please check your config:”, e);
}
return con;
}
@Override
public void close(AutoCloseable... autoCloseable) {
for (AutoCloseable closeable : autoCloseable) {
if (closeable != null){
try {
closeable.close();
}catch (Exception e){
log.error("Close fail:", e);
}finally {
closeable = null;
}
}
}
}
@Override
public boolean baseSqlExecute(Connection con, String sql, String... params) {
boolean is = false;
ClickHousePreparedStatement pst = null;
if (con == null){
log.error("Connection is empty");
return is;
}
try {
// 获取控制台
pst = (ClickHousePreparedStatement) con.prepareStatement(sql); // 需要做安全处理
for (int i = 0; i < params.length; i++) {
pst.setObject(i+1, params[i]);
}
is = pst.execute();
}catch (SQLException e){
log.error("SQLException:", e);
}finally {
// close(pst, con);
}
return is;
}
@Override
public boolean insert(Connection con, String sql, String... params) {
return false;
}
@Override
public boolean delete(Connection con, String sql, String... params) {
return false;
}
@Override
public List<Map<String, Object>> queryResultSet(Connection con, String sql, String... params) {
List<Map<String, Object>> list = new ArrayList<>();
ResultSet r = null;