简体   繁体   English

如何将两个数组列表组合为一个数组列表并将其传递给过程

[英]How to combine two arraylists into one arraylist and pass it to procedure

I need your help and assistant in combining two arraylists into a new arraylist to be passed to a procedure. 在将两个数组列表组合成一个新的数组列表以传递给过程时,我需要您的帮助和助手。 Once the user selected certificates from Type A list, they will be stored in: 用户从Type A列表中选择证书后,它们将存储在:

private List<String> selectedTypeACertificates = new ArrayList<String>();

And once the user selected certificates from Type B, they will be stored in: 一旦用户从类型B中选择了证书,它们将被存储在:

private List<String> selectedTypeBCertificates = new ArrayList<String>();

Now in the procedure I need to pass only two String variables. 现在,在该过程中,我只需要传递两个String变量。 One is the Staff Code and the other is the selectedTypes for both Types A & B. 一个是人员代码,另一个是A和B类型的selectedTypes。

CallableStatement callableStatement =
    connection.prepareCall("{call processCertificates(?, ?)}");

callableStatement.setInt(1, Staff_Code);
callableStatement.setString   (2, ); //I need to pass selectedTypeBCertificates & selectedTypeACertificates

You can add all elements from both lists to a newly created one, and then use a StringBuilder to build a string in order to pass it to the procedure: 您可以将两个列表中的所有元素添加到一个新创建的元素中,然后使用StringBuilder构建一个字符串,以将其传递给过程:

List<String> selectedCertificates = new ArrayList<String>();
selectedCertificates.addAll(selectedTypeACertificates);
selectedCertificates.addAll(selectedTypeBCertificates);

StringBuilder sb = new StringBuilder();
for (String cert : selectedCertificates)
{
    sb.append(cert);
    sb.append("','");
}

String certificatesString = sb.toString();
if(!certificatesString.isEmpty()) {
    certificatesString = "'" + certificatesString.substring(0, certificatesString.lastIndexOf(",")) + "'";
}

The last couple of lines are there to remove the trailing comma. 最后两行用于删除结尾的逗号。 Of course you might need to change this if the procedure expects the string in a different format. 当然,如果过程要求使用不同格式的字符串,则可能需要更改此设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM