简体   繁体   English

Java从列表中提取值并将其作为列名称包含在SQL查询中

[英]Java Extracting the values from a list and include it as column names in a sql query

I have a function hasColumnBeenIdentified() which takes an arraylist()(columnNameList) as one of the arguments to this method. 我有一个函数hasColumnBeenIdentified(),它将arraylist()(columnNameList)作为此方法的参数之一。 The logic the method performs is that the method has to connect to the database and execute a Query. 该方法执行的逻辑是该方法必须连接到数据库并执行查询。 The Query that it executes is a select query in which the column names will be retrieved from the arraylist which is passed as an argument to this method. 它执行的查询是一个select查询,其中将从arraylist中检索列名,该arraylist作为参数传递给此方法。 The Query will look like below 查询将如下所示

 String query = "Select columnNameList FROM "+ currTableName + " " + 
                "WHERE ROWNUM <= " + numberOfSamplesForInstaceMatching;

I have two issues here. 我这里有两个问题。

  1. how to extract the values from the list(columnNameList) and use it in the query and execute the query. 如何从列表中提取值(columnNameList)并在查询中使用它并执行查询。 Ie the ColumnNameList contains the list of columns whose values needs to be retrieved from the db. 即ColumnNameList包含需要从db中检索其值的列的列表。 I need to know how to insert the columnnames in the query. 我需要知道如何在查询中插入列名。
  2. After executing the above query, once we get the result set, I would like to know how to extract the column values from the resultset and store it in a hashmap. 执行上述查询后,一旦我们得到结果集,我想知道如何从结果集中提取列值并将其存储在hashmap中。

Note: The method hasColumnBeenIdentified() will get invoked multiple times ie it will get invoked based on the number of tables in a particular schema. 注意:方法hasColumnBeenIdentified()将被多次调用,即它将根据特定模式中的表的数量进行调用。 Hence I cannot customize the resultset nor the columnNamelist, since for each table, the number of columns will be vary. 因此,我无法自定义结果集,也无法自定义columnNamelist,因为对于每个表,列数将有所不同。

Any help on this is much appreciated. 对此有任何帮助非常感谢。 Thanks in Advance 提前致谢

As per my Understand you need to coombine your column name list in query.For this you can iterate your list create comma separated string of your column name. 根据我的理解,您需要在查询中将列名列表组合在一起。为此,您可以迭代列表创建逗号分隔的列名字符串。

String columnStr = "";
for(String columnName : yourList<>)  
{  
   columnStr += columnName+",";  
}  
columnStr = columnStr.substring(0, columnStr.length()-1);  

Replace this string in your query. 在查询中替换此字符串。

String query = "Select "+columnStr+" FROM "+ currTableName + " " + 
            "WHERE ROWNUM <= " + numberOfSamplesForInstaceMatching;  

For getting column name back from ResultSet you need ResultSetMetaData like given below. 要从ResultSet获取列名,您需要ResultSetMetaData如下所示。

ResultSet rs = ...; //Your ResultSet
ResultSetMetaData rsm = rs.getMetaData();
String columnName = rsm.getColumnName("YourColumnIndex"); //Index start from 1  

For storing your result in HashMap you first need to decide howto store it into HashMap. 要将结果存储在HashMap中,首先需要决定如何将其存储到HashMap中。

Edit : 编辑:
You may use external library to join your list into String, one of them is 您可以使用外部库将列表加入String,其中一个是
Apache's commons lang Apache的公共语言

Like this : String columnNames = StringUtils.join(YOUR_LIST,","); 像这样: String columnNames = StringUtils.join(YOUR_LIST,",");

You can create a String with the columnnames from the list and use this string in the query. 您可以使用列表中的列名创建一个String,并在查询中使用此字符串。 Eg you can use following: 例如,你可以使用以下:

String columnsString  = org.apache.commons.lang.StringUtils.join(ColumnNameList, ",");
String query = "Select ColumnNameList FROM "+ currTableName + " " + 
            "WHERE columnName IN ( " + columnsString + ")";

Another possibility withou using a external library is to create the string in a for-loop: 使用外部库的另一种可能性是在for循环中创建字符串:

StringBuilder builder = new StringBuilder();
builder.append(list.remove(0));

for( String s : ColumnNameList) {
    builder.append(", ");
    builder.append(s);
}

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

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