package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class getParameter {
public ArrayList<Map<String,ResultSet>> getParameter_test(String api_code_string) throws SQLException{
List<String> apiCodeList = Arrays.asList(api_code_string.split(","));
System.out.println("##########开始对api_code进行排序##########");
Connection c=null;
Map<String,Integer> sorted_map=new HashMap();
for(int i=0;i<apiCodeList.size();i++) {
int tmp;
String apiCode = apiCodeList.get(i);
try {
Class.forName("org.postgresql.Driver");
ResultSet rs2 = null;
c = DriverManager.getConnection("xxxxxxx", "xxxxxxx",
"xxxxxxx");
Statement stmt2=c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs2=stmt2.executeQuery("select api_weight from xxx where api_code='"
+apiCode+"';");
if(rs2.next()) {
rs2.first();
tmp=rs2.getInt(1);
sorted_map.put(apiCode, tmp);
// System.out.println("数据长这样"+rs2.getInt(1));
}else {
sorted_map.put(apiCode,0);
}
rs2.close();
stmt2.close();
}catch(Exception e) {
e.printStackTrace();
}finally {c.close();};
}
ArrayList<Map.Entry<String,Integer>> list = new ArrayList<>(sorted_map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());//升序,前边加负号变为降序
}
});
List<String> apiCodeListSored=new ArrayList<>();
Iterator<Map.Entry<String, Integer>> iterator = list.iterator();
for(Map.Entry<String, Integer> m : list){
System.out.println(m.getKey()+"="+m.getValue());
apiCodeListSored.add(m.getKey());
}
System.out.println(apiCodeListSored);
ArrayList<Map<String,ResultSet>> list2=new ArrayList<>();
for (int i = 0; i < apiCodeListSored.size(); i++) {
String apiCode = apiCodeListSored.get(i);
Map<String,ResultSet> result_map=new HashMap();
try {
Class.forName("org.postgresql.Driver");
ResultSet rs = null;
c = DriverManager.getConnection("xxxxxxx", "xxxxxxx",
"xxxxxxx");
Statement stmt = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(
"select api_url,mongodb_host,mongodb_port,mongodb_dbname,mongodb_collection from xxxx where api_code='"
+ apiCode + "';");
result_map.put(apiCode, rs);
list2.add(result_map);
rs.close();
stmt.close();
}catch(Exception e) {
e.printStackTrace();;
}finally {
c.close();
}
}
return list2;
}
public static void main(String args[]) throws SQLException {
ArrayList<Map<String,ResultSet>>s=null;
getParameter gp=new getParameter();
s=gp.getParameter_test("371,364,888,666,717,918,819,999,456,909");
for (int i=0;i<s.size();i++) {
Map<String,ResultSet> m=new HashMap<>();
m=s.get(i);
for (Map.Entry<String, ResultSet> entry : m.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
}
}