0

我的结果如下:

tablename   columnname     size    order
employee    name            25      1
employee    sex             25      2
employee    contactNumber   50      3
address     name            25      4
address     street          25      5
address     country         25      6

这是我的 java 代码,用于根据table name

public void getReportQuery() {
    String tablename="",columnname="";
    int size=0,order=0;
    boolean isCustomised=true;
    StringBuffer sb = new StringBuffer();
    Map<String, Map<String, String>> reportQueryMap = new HashMap<String, Map<String, String>>();
    List<String> reportQueryTableNameList = new ArrayList<String>();
    Connection connection = getConnection();
    if (connection != null) {
       try {
         sb.append("SELECT rmaster.tablename,rmaster.columnname,r.size,r.order FROM report_customise_child r,report_customise_master rmaster where r.isactive='y' and rmaster.id=r.masterid; ");
         PreparedStatement reportQueryPS = connection.prepareStatement(sb.toString());
                ResultSet reportQuery_rst= reportQueryPS.executeQuery();
    if(reportQuery_rst!=null){
     while (reportQuery_rst.next()) {
       tablename = reportQuery_rst.getString("tablename");
        if (!reportQueryTableNameList.contains(tablename)) {
            reportQueryTableNameList.add(tablename);
        System.out.println("tablename : "+tablename);
                        }
                            columnname = reportQuery_rst.getString("columnname");
                            System.out.println(" columnname : "+columnname);

                            //size = reportQuery_rst.getInt("size");
                            //order = reportQuery_rst.getInt("order");
                  }
                }else{
                    isCustomised=false;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    closeConnection(connection, null, null);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        } else {
            System.out.println("Connection not Established. Please Contact Vendor");
        }
    }

我真的不知道如何对值进行分组,我想从上面的结果中构造查询。

请帮助我对值进行分组,我将从分组值中构造查询。

我需要像下面这样分组

tablename1
columnmaes

tablename2
columnnames

tablenameN
columnnames

请帮助我实现这一目标,

问候

4

1 回答 1

1

您可以让Map<String, List<String>>where key 是表名, value 是列名列表。

Map<String, List<String>> resultsMap = new HashMap<String, List<String>>();
ResultSet reportQuery_rst= reportQueryPS.executeQuery();
if(reportQuery_rst!=null){
   while (reportQuery_rst.next()) {
      String tableName = reportQuery_rst.getString("tablename");
      List<String> columns = resultsMap.get(tableName); 
      if(columns == null ) { 
          columns = new ArrayList<String>();
          resultMap.put(tableName, columns);
      }
      columns = resultsMap.get(tableName);
      String columnName = reportQuery_rst.getString("columnname");
      columns.add(columnName);
   }
}

现在resultsMap将有tableNames vs ColumnNames。如果要存储订单和大小等其他信息,则需要创建一个 bean 并将它们存储在表名中,例如Map<String, List<YourBean>>.

更新

现在,您想准备不同的查询,其中包含不同的表名resultMap

List<String> queries = new ArrayList<String>();
for(Entry<String, List<String>> resultEntry : resultMap.entrySet()) {
   String tableName = resultEntry.getKey();
   List<String> columns = resultEntry.getValue();
   StringBuilder query = new StringBuilder(" select ");
   int i = 0;
   for(String column : columns) {
       query.append(column);
       if(i != columns.size()) {
          query.append(", ");
       } 
       i++;
   }
   query.append(" from ").append(tableName);
   queries.add(query.toString());
} 

Now queries will have all the require queries, As you have several queries, You need to take care of Connection pooling and all, Try to use something like JDBC Templates which will provide most of the common required operations like DataSource, Connection pool, Automatic Bean Mapping etc. Hope this helps your.

于 2012-09-03T11:10:07.057 回答