如何在java中的sql查询字符串中放置占位符?

问题描述 投票:0回答:1

我需要使用占位符更改“22-05-2024”以动态传递参数,然后将字符串传递给 searchLimit 进行查询。我能怎么做?我需要传递方法的参数,然后...? 谢谢大家的帮助,抱歉英语不好!

我尝试用 ? 更改日期但不知道是否正确

private static final String QUERY_5 = "select * from ESTERO.VW_EST_MARGINI_INVIO_MIDA_Q where 1=1 AND data_riferimento = to_date('22-05-2024','dd-mm-yyyy') and UPCV_CODE = 'UCV_EDCROEETLDFI' and mercato = 'MGP' and data_invio_forecast is not null";
    @Datasource(datasourceKey = EDatasourceKey.DAMASCO)
    public List<UpView> searchLimit()
    {
        try {
            long startTime = System.currentTimeMillis();
            log.debug("call searchLimit(): |{}|", startTime);

            StringBuilder sql = new StringBuilder(QUERY_5);

            List<UpView> result = dataAccessService.search(UpView.class, sql.toString(), null, true, true);

            log.info("end searchLimit(): |{}|", System.currentTimeMillis() - startTime);

            if(Assorted.notNull(result)) {
                return result;
            }
            else {
                return null;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
java sql mysql spring-boot
1个回答
0
投票

一些

jshell
输入/输出。我希望它是不言自明的 - 如果不是,请说:

jshell> private static final String QUERY_5 = "select * from ESTERO.VW_EST_MARGINI_INVIO_MIDA_Q where 1=1 AND data_riferimento = to_date('%s','dd-mm-yyyy') and UPCV_CODE = 'UCV_EDCROEETLDFI' and mercato = 'MGP' and data_invio_forecast is not null";
QUERY_5 ==> "select * from ESTERO.VW_EST_MARGINI_INVIO_MIDA_Q ... nvio_forecast is not null"

jshell> QUERY_5.formatted(DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.US).format(LocalDate.now()));
$2 ==> "select * from ESTERO.VW_EST_MARGINI_INVIO_MIDA_Q where 1=1 AND data_riferimento = to_date('23-05-2024','dd-mm-yyyy') and UPCV_CODE = 'UCV_EDCROEETLDFI' and mercato = 'MGP' and data_invio_forecast is not null"
© www.soinside.com 2019 - 2024. All rights reserved.