I have a LinkedHahMap map1 whcih holds key as my beam_current which is of double type and value as my logtime which is of string type. Now I want to use this map1.keySet() in my sql query as-
Set<Double> arr= map1.keySet();
String vs2="select b.beam_current, b.beam_energy where
b.logtime between '"+first+"' and '"+last+"' and b.beam_current in('"+arr+"')";
But when I use arr which holds value of map1 key ,**nothing is being displayed.**Can't we use map1.KeySet() method in sql query or I'm implemting it in wrong way??
First convert your map keys into comma separated string and then use it in your query.
List<Double> slist = new ArrayList<Double>(map1.keySet());
String s = StringUtils.join(slist, ',');
String vs2="select b.beam_current, b.beam_energy where
b.logtime between '"+first+"' and '"+last+"' and
b.beam_current in('"+s+"')";
If you don't want to depend on external libs (like StringUtils.join), you can do it manually:
public static void main(String[] args) {
HashMap<Double, String> map1 = new HashMap<Double, String>();
map1.put(1.5, "");
map1.put(2.5, "");
String first = "first";
String last = "last";
String query = buildQuery(map1, first, last);
System.out.println(query);
}
private static String buildQuery(HashMap<Double, String> map1, String first, String last) {
StringBuilder sb = new StringBuilder();
sb.append("select b.beam_current, b.beam_energy where b.logtime between '");
sb.append(first);
sb.append("' and '");
sb.append(last);
sb.append("' and b.beam_current in (");
String separator = "";
for (Double val : map1.keySet()) {
sb.append(separator);
sb.append(val);
separator = ", ";
}
sb.append(")");
return sb.toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.