当前位置: 首页 > news >正文

查询Hologres或postgresql中的数据

因Hologres使用postgresql的语法.所以两者查询一样.

方案1:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;/*** 一个使用简单连接池管理PostgreSQL连接的工具类。*/
public class PostgresConnectionUtil {private static final String URL = "jdbc:postgresql://hgprecn-cn-******"; //holo数据库链接地址private static final String USER = "xxxx"; // 数据库登录用户private static final String PASSWORD = "yyyy"; // 数据库用户密码private static final int INITIAL_POOL_SIZE = 10; // 初始化连接数private static final List<Connection> connectionPool = new ArrayList<>(INITIAL_POOL_SIZE);private static final List<Connection> usedConnections = new ArrayList<>();static {try {for (int i = 0; i < INITIAL_POOL_SIZE; i++) {connectionPool.add(createConnection());}} catch (SQLException e) {e.printStackTrace();}}/*** 创建一个新的PostgreSQL数据库连接。** @return 一个新的数据库连接* @throws SQLException 如果发生数据库访问错误*/private static Connection createConnection() throws SQLException {return DriverManager.getConnection(URL, USER, PASSWORD);}/*** 从连接池中获取一个连接。** @return 一个PostgreSQL数据库连接* @throws SQLException 如果发生数据库访问错误*/public static synchronized Connection getConnection() throws SQLException {if (connectionPool.isEmpty()) {connectionPool.add(createConnection());}Connection connection = connectionPool.remove(connectionPool.size() - 1);usedConnections.add(connection);return connection;}/*** 将连接释放回连接池。** @param connection 要释放的连接*/public static synchronized void releaseConnection(Connection connection) {connectionPool.add(connection);usedConnections.remove(connection);}/*** 关闭连接池中的所有连接。*/public static synchronized void closeAllConnections() {for (Connection connection : connectionPool) {try {connection.close();} catch (SQLException e) {e.printStackTrace();}}for (Connection connection : usedConnections) {try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}/*** 获取可用连接的数量。** @return 可用连接的数量*/public static int getAvailableConnections() {return connectionPool.size();}
}


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class PostgresConnectionExample {public static void main(String[] args) {
//        // 插入操作
//        String insertQuery = "INSERT INTO test_table (name) VALUES (?)";
//        try (Connection connection = PostgresConnectionUtil.getConnection();
//             PreparedStatement preparedStatement = connection.prepareStatement(insertQuery)) {
//
//            preparedStatement.setString(1, "John Doe");
//            int rowsAffected = preparedStatement.executeUpdate();
//            System.out.printf("Inserted %d row(s)%n", rowsAffected);
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//        // 更新操作
//        String updateQuery = "UPDATE test_table SET name = ? WHERE id = ?";
//        try (Connection connection = PostgresConnectionUtil.getConnection();
//             PreparedStatement preparedStatement = connection.prepareStatement(updateQuery)) {
//
//            preparedStatement.setString(1, "Jane Doe");
//            preparedStatement.setInt(2, 1);
//            int rowsAffected = preparedStatement.executeUpdate();
//            System.out.printf("Updated %d row(s)%n", rowsAffected);
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }// 查询操作
//        String selectQuery = "SELECT action_date,company_key FROM t_ads_application_company_persona_erp_trend";String selectQuery = "select * from test_table limit 3";try (Connection connection = PostgresConnectionUtil.getConnection();PreparedStatement preparedStatement = connection.prepareStatement(selectQuery);ResultSet resultSet = preparedStatement.executeQuery()) {while (resultSet.next()) {
//                String actionDate = resultSet.getString("action_date");
//                Long companyKey = resultSet.getLong("company_key");
//                System.out.printf("ID: %d, Name: %s%n", actionDate, companyKey);Long id = resultSet.getLong("id");Long teamId = resultSet.getLong("team_id");System.out.printf("ID: %d, Name: %s%n", id, teamId);}} catch (SQLException e) {e.printStackTrace();}//        // 删除操作
//        String deleteQuery = "DELETE FROM test_table WHERE id = ?";
//        try (Connection connection = PostgresConnectionUtil.getConnection();
//             PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery)) {
//
//            preparedStatement.setInt(1, 1);
//            int rowsAffected = preparedStatement.executeUpdate();
//            System.out.printf("Deleted %d row(s)%n", rowsAffected);
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            // 释放连接回连接池
//            PostgresConnectionUtil.closeAllConnections();
//        }}
}

方案2:

import com.hupun.luban.holo.datasource.HoloDataSourceProperty;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.Date;
import java.util.*;

@Component
public class HologresUtils {

    private static final Log logger = LogFactory.getLog(HologresUtils.class);
    private BasicDataSource holoDataSource;

    @Autowired
    private HoloDataSourceProperty holoDataSourceProperty;

    @PostConstruct
    public BasicDataSource initHoloDataSource() {
        if (holoDataSource == null) {
            BasicDataSource ds = new BasicDataSource();
            ds.setUrl(holoDataSourceProperty.getUrl());
            ds.setUsername(holoDataSourceProperty.getUsername());
            ds.setPassword(holoDataSourceProperty.getPassword());            
            ds.setDriverClassName("org.postgresql.Driver");
            ds.setMinIdle(3); // 最小空闲数
            ds.setMaxIdle(5); //最大空闲数
            ds.setMaxOpenPreparedStatements(100);
            ds.setMaxTotal(5); //最大连接数
            ds.setMinEvictableIdleTimeMillis(300000); // 最小空闲时间
            ds.setMaxConnLifetimeMillis(7200000); // 最大空闲时间
            ds.setTimeBetweenEvictionRunsMillis(60000); // 休眠时间
            ds.setRemoveAbandonedOnBorrow(true);
            ds.setRemoveAbandonedTimeout(300);
            ds.setLogAbandoned(true);
            ds.setValidationQuery("SELECT 1");
            ds.setValidationQueryTimeout(2);
            ds.setDefaultQueryTimeout(120); // 默认查询超时时间
            holoDataSource = ds;
        }
        return holoDataSource;
    }

    public BasicDataSource getHoloDataSource() {
        return holoDataSource;
    }

    public void setHoloDataSource(BasicDataSource holoDataSource) {
        this.holoDataSource = holoDataSource;
    }

    public HoloDataSourceProperty getHoloDataSourceProperty() {
        return holoDataSourceProperty;
    }

    public void setHoloDataSourceProperty(HoloDataSourceProperty holoDataSourceProperty) {
        this.holoDataSourceProperty = holoDataSourceProperty;
    }

    /**
     * 查询
     * @param business 业务名称
     * @param sql      查询语句
     * @param clazz    结果类型
     * @param timeout  超时时间
     * @return 结果列表
     */
    public <T> List<T> query(String business, String sql, Class<T> clazz, int timeout) {
        List<T> results = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = holoDataSource.getConnection();
            statement = connection.createStatement();
            statement.setQueryTimeout(timeout);
            resultSet = statement.executeQuery(sql);

            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            if (columnCount == 1) {
                results = handleSingleColumnResult(resultSet, metaData, clazz);
            } else {
                results = handleMultiColumnResult(resultSet, clazz);
            }
        } catch (Exception e) {
            logger.error(business + "holo查询异常,sql={"+sql+"}", e);
            cancelQuery(statement);
        } finally {
            closeResources(resultSet, statement, connection);
        }

        return results;
    }

    private <T> List<T> handleSingleColumnResult(ResultSet resultSet, ResultSetMetaData metaData, Class<T> clazz) throws SQLException {
        List<T> results = new ArrayList<>();
        String columnName = metaData.getColumnName(1);

        while (resultSet.next()) {
            Object value = extractValue(resultSet, columnName, clazz);
            @SuppressWarnings("unchecked")
            T castedValue = (T) value;
            results.add(castedValue);
        }
        return results;
    }

    private <T> List<T> handleMultiColumnResult(ResultSet resultSet, Class<T> clazz) throws Exception {
        List<T> results = new ArrayList<>();

        if (Map.class.isAssignableFrom(clazz)) {
            @SuppressWarnings("unchecked")
            List<T> mapResults = (List<T>) queryAsMap(resultSet);
            results.addAll(mapResults);
        } else {
            Field[] fields = clazz.getDeclaredFields();
            Map<String, Field> fieldMap = new HashMap<>();
            for (Field field : fields) {
                fieldMap.put(field.getName(), field);
                field.setAccessible(true);
            }

            while (resultSet.next()) {
                T instance = clazz.getDeclaredConstructor().newInstance();
                for (Map.Entry<String, Field> entry : fieldMap.entrySet()) {
                    String fieldName = entry.getKey();
                    Field field = entry.getValue();
                    Object value = extractValue(resultSet, fieldName, field.getType());
                    field.set(instance, value);
                }
                results.add(instance);
            }
        }
        return results;
    }

    private Object extractValue(ResultSet resultSet, String columnName, Class<?> fieldType) throws SQLException {
        Object value;
        if (fieldType == String.class) {
            value = resultSet.getString(columnName);
        } else if (fieldType == Integer.class || fieldType == int.class) {
            value = resultSet.getInt(columnName);
        } else if (fieldType == Long.class || fieldType == long.class) {
            value = resultSet.getLong(columnName);
        } else if (fieldType == Double.class || fieldType == double.class) {
            value = resultSet.getDouble(columnName);
        } else if (fieldType == Boolean.class || fieldType == boolean.class) {
            value = resultSet.getBoolean(columnName);
        } else if (fieldType == Date.class) {
            value = resultSet.getDate(columnName);
        } else if (fieldType == Timestamp.class) {
            value = resultSet.getTimestamp(columnName);
        } else {
            logger.error("类型不匹配");
            return null;
        }
        return value;
    }

    private void cancelQuery(Statement statement) {
        if (statement != null) {
            try {
                statement.cancel();
            } catch (SQLException e) {
                logger.error("holo取消查询异常,error={"+e.getMessage()+"}");
            }
        }
    }

    private void closeResources(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ignored) {
        }
    }

    private List<Map<String, Object>> queryAsMap(ResultSet resultSet) throws SQLException {
        List<Map<String, Object>> results = new ArrayList<>();
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();

        while (resultSet.next()) {
            Map<String, Object> row = new HashMap<>();
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnName(i);
                Object value = resultSet.getObject(i);
                row.put(columnName, value);
            }
            results.add(row);
        }
        return results;
    }
}
 

相关文章:

  • 即插即用模块(3) -LSK 特征提取
  • Spring--统一数据返回格式与统一异常处理
  • 将 MySQL 8 主从复制延迟优化到极致
  • 如何在iStoreOS DHCP中排除特定IP地址
  • zkPass案例实战之合约篇
  • 【论文#目标检测】Attention Is All You Need
  • 【泊松过程和指数分布】
  • 力扣DAY63-67 | 热100 | 二分:搜索插入位置、搜索二维矩阵、排序数组查找元素、搜索旋转排序数组、搜索最小值
  • OpenCV 图形API(52)颜色空间转换-----将 NV12 格式的图像数据转换为 RGB 格式的图像
  • 计算机视觉基础
  • 提高Spring Boot开发效率的实践
  • MsQuick编译和使用
  • c++概念——模板的进阶讲解
  • django软件开发招聘数据分析与可视化系统设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
  • 香港科技大学广州|金融科技学域博士招生宣讲会—南开大学专场
  • ThinkPHP快速使用手册
  • VUE的创建
  • 【C语言】文本操作函数fgetc、fputc、fgets、fputs、fprintf、fscanf、fread、fwrite
  • 【Linux应用】RADXA ZERO 3快速上手:镜像烧录、串口shell、外设挂载、WiFi配置、SSH连接、文件交互
  • JavaEE学习笔记(第二课)
  • 沂水县委书记陈士贤,跨市履新泰安市委常委、组织部部长
  • 这场宣介会,重庆市委书记和中联部部长同台为外宾答疑解惑
  • 解密帛书两千年文化传承,《帛书传奇》央视今晚开播
  • 中国建设银行原党委委员、副行长章更生严重违纪违法被开除党籍
  • 特朗普特使将赴俄见普京,俄方:美俄间谈判艰难且耗时
  • 北京地铁5号线仗义执言女乘客发文:同理心无比重要,希望就此平息