private JSONArray executeStatementToJson(
				PreparedStatement stmt,
				List<SelectedColumn> selectFields) throws Exception {
//logger.info("about to execute: " + stmt.toString());
		if( stmt.execute() ) {
			// There's a ResultSet to be had
			ResultSet rs = stmt.getResultSet();

			JSONArray array = new JSONArray();
			try {
				Map<Integer,JSONObject> contributorMap = new HashMap<Integer,JSONObject>();
				while (rs.next()) {
					JSONObject obj = new JSONObject();

					int index = 1;
					for(int loop=0; loop<selectFields.size(); ++loop) {
						SelectedColumn selCol = selectFields.get(loop);
//logger.info("field: " + selCol.getName() + " (" + selCol.getType() + ")");
					
						if (! "contributor_id".equalsIgnoreCase(selCol.getName())) {
							if( SelectedColumn.Type.INTEGER == selCol.getType() ) {
								obj.put(selCol.getName(),rs.getInt(index));
								++index;

							} else if( SelectedColumn.Type.STRING == selCol.getType() ) {
								obj.put(selCol.getName(),rs.getString(index));
								++index;

							} else if ( SelectedColumn.Type.DATE == selCol.getType() ) {
								Date date = rs.getDate(index);
								if (null != date) {
									String dateString = dateFormatter.format(date);
									obj.put(selCol.getName(), dateString);
								}
								++index;

							} else {
								throw new Exception("Unkown selected column type");
							}
						} else {
							// Convert contributor id into user info
							int contribId = rs.getInt(index);
							JSONObject userInfo = fetchContributorFromIdWithCache(
									contribId, contributorMap);
							if (null != userInfo) {
								obj.put("contributor", userInfo);
							}
							++index;
						}
					}
					
					array.put(obj);
				}
			} catch (Exception je) {
				throw new ServletException("Error while executing statement",je);
			}
			
			return array;
			
		} else {
			// indicates an update count or no results - this must be no results
			throw new Exception("Query returned no results");
		}
	}
private JSONArray executeStatementToJson ( PreparedStatement stmt , List < SelectedColumn > selectFields ) throws Exception { if ( stmt . execute ( ) ) { ResultSet rs = stmt . getResultSet ( ) ; JSONArray array = new JSONArray ( ) ; try { Map < Integer , JSONObject > contributorMap = new HashMap < Integer , JSONObject > ( ) ; while ( rs . next ( ) ) { JSONObject obj = new JSONObject ( ) ; int index = 1 ; for ( int loop = 0 ; loop < selectFields . size ( ) ; ++ loop ) { SelectedColumn selCol = selectFields . get ( loop ) ; if ( ! "contributor_id" . equalsIgnoreCase ( selCol . getName ( ) ) ) { if ( SelectedColumn . Type . INTEGER == selCol . getType ( ) ) { obj . put ( selCol . getName ( ) , rs . getInt ( index ) ) ; ++ index ; } else if ( SelectedColumn . Type . STRING == selCol . getType ( ) ) { obj . put ( selCol . getName ( ) , rs . getString ( index ) ) ; ++ index ; } else if ( SelectedColumn . Type . DATE == selCol . getType ( ) ) { Date date = rs . getDate ( index ) ; if ( null != date ) { String dateString = dateFormatter . format ( date ) ; obj . put ( selCol . getName ( ) , dateString ) ; } ++ index ; } else { throw new Exception ( "Unkown selected column type" ) ; } } else { int contribId = rs . getInt ( index ) ; JSONObject userInfo = fetchContributorFromIdWithCache ( contribId , contributorMap ) ; if ( null != userInfo ) { obj . put ( "contributor" , userInfo ) ; } ++ index ; } } array . put ( obj ) ; } } catch ( Exception je ) { throw new ServletException ( "Error while executing statement" , je ) ; } return array ; } else { throw new Exception ( "Query returned no results" ) ; } }
This method executes a prepared SQL statement and returns a JSON
array that contains the result.
@param stmt Prepared SQL statement to execute
@param selectFields Column to return in results
@return A JSONArray containing the requested information
@throws Exception