gpt4 book ai didi

java - 如何用不同的值替换多个字符?

转载 作者:行者123 更新时间:2023-11-30 05:38:35 25 4
gpt4 key购买 nike

我有一个字符串,我想替换其中的字符“?”我有一个包含要替换的值的数组。
这是我的字符串:

FROM lineorder A INNER JOIN date B 
ON (B.d_datekey = A.lo_orderdate)
WHERE
(A.lo_discount >= ? AND A.lo_discount <= ?) AND (A.lo_quantity < ?)
AND (B.d_year = ?)

这是我的数组,其值为 [1, 3, 25, 1993]

我想得到以下结果:

FROM lineorder A INNER JOIN date B 
ON (B.d_datekey = A.lo_orderdate)
WHERE
(A.lo_discount >= 1 AND A.lo_discount <= 3) AND (A.lo_quantity < 25)
AND (B.d_year = 1993)

我该怎么做?

最佳答案

如果您使用 JDBC,并且有一个PreparedStatement,那么您可以执行以下操作:

String sql = "FROM lineorder A INNER JOIN date B \n" +
"ON (B.d_datekey = A.lo_orderdate) \n" +
"WHERE \n" + " (A.lo_discount >= ? AND A.lo_discount <= ?) AND (A.lo_quantity < ?) \n" +
" AND (B.d_year = ?)";

PreparedStatement preparedStatement = connection.prepareStatement(sql);

int[] arrayOfInts = {1,3,25,1993};

for(int i = 0; i < arrayOfInts.length; i++) {
preparedStatement.setInt(i + 1, arrayOfInts[i]); // the i index goes from the first "?" to the last, setting their values with the array value at that index. i + 1 because PreparedStatements indexes start from 1.
}

关于java - 如何用不同的值替换多个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com