Пример #1
0
 private String transResultsetToString(SqlRowSet sqlRowSet) throws SQLException {
   SqlRowSetMetaData sqlRowSetMetaData = sqlRowSet.getMetaData();
   int colCount = sqlRowSetMetaData.getColumnCount();
   StringBuffer sb = new StringBuffer();
   while (sqlRowSet.next()) {
     for (int i = 1; i <= colCount; i++) {
       int type = sqlRowSetMetaData.getColumnType(i);
       sb.append(getValue(sqlRowSet, i, type));
       if (i < colCount) {
         sb.append(";");
       }
     }
     sb.append("\n");
   }
   return sb.toString();
 }
  /**
   * this function will return a jsonstring given a resultset in a format that is required for
   * WTF.Grid just use this inside the ur jsp file and set the url for the grid datastore as that
   * jsp file
   */
  public JSONObject GetJsonForGrid(SqlRowSet rs) {
    JSONObject jb = null;
    try {
      SqlRowSetMetaData rsmd = rs.getMetaData();
      JSONArray jArr = new JSONArray();
      while (rs.next()) {
        JSONObject jobj = new JSONObject();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
          if (rs.getObject(i) != null) jobj.put(rsmd.getColumnName(i), rs.getObject(i));
          else jobj.put(rsmd.getColumnName(i), "");
        }
        jArr.put(jobj);
      }
      if (jArr.length() > 0) jb = new JSONObject().put("data", jArr);
    } catch (Exception ex) {
      System.out.println("exception -->" + ex);
    }

    return jb;
  }
  private List<ResultsetRowData> fillDatatableResultSetDataRows(final String sql) {

    final SqlRowSet rs = this.jdbcTemplate.queryForRowSet(sql);

    final List<ResultsetRowData> resultsetDataRows = new ArrayList<ResultsetRowData>();

    final SqlRowSetMetaData rsmd = rs.getMetaData();

    while (rs.next()) {
      final List<String> columnValues = new ArrayList<String>();
      for (int i = 0; i < rsmd.getColumnCount(); i++) {
        final String columnName = rsmd.getColumnName(i + 1);
        final String columnValue = rs.getString(columnName);
        columnValues.add(columnValue);
      }

      final ResultsetRowData resultsetDataRow = ResultsetRowData.create(columnValues);
      resultsetDataRows.add(resultsetDataRow);
    }

    return resultsetDataRows;
  }