查询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;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值