| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 
 | public class QuerySql extends HttpServlet 
{
	// Constants
	private static final long serialVersionUID = 1L;
	private static final String QUERY_FILE_PATH = "/path/to/getSql.properties";
 
	// Attributes
	private Properties queryProps;
	private HashMap allQueries;	
	private JSONObject resultList;
	private JSONArray statham;
 
    public QuerySql() {
        super();
        // TODO Auto-generated constructor stub
    }
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		queryProps  = new Properties();
		allQueries = new HashMap();
		statham = new JSONArray();
 
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
 
		// Read the Properties
		AllSettings.readProps();
		readPropsQueries();
 
		// Get the query
		String aStatement = (String) allQueries.get(request.getParameter("sqlStatement"));
 
		// Get the params
		ArrayList params = new ArrayList();
		Map allFormParameters = request.getParameterMap();
        Set set = allFormParameters.keySet();
        Iterator iterator = set.iterator();
        String tmp = null;
 
        // Get the params from the web form
        while (iterator.hasNext()) 
        {
        	tmp = (String) iterator.next();
        	if (tmp.matches("param[0-9]+"))
        	{
        		String currentParamValue = ((String[])allFormParameters.get(tmp))[0];
        		if(!currentParamValue.equals(""))
        			params.add(currentParamValue);
        	}
        }
 
        // Check if the Query was found
        if(aStatement == null) // Not found
        {
        	resultList = new JSONObject();
        	try 
        	{
				Set s = allQueries.keySet();
				Iterator i = s.iterator();
				while(i.hasNext())
				{
					Object key = i.next();
					Object value = allQueries.get(key);
 
					resultList.put((String) key, value);
				}				
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
 
			String callBack = request.getParameter("jsoncallback");
			if(callBack != null)
				out.print(callBack + "(" + resultList + ");");
			else
				out.print(resultList);
			out.close();
        }
        else // Found the query
        {
	        // Replace the params in the query
	        String newStatement = aStatement;
 
	        // Loop over all the PARAMS
	        for(int i = 0; i < params.size(); i++)
	        {
	        	String multipleParams = "";
	        	String currentParam = (String) params.get(i);
	        	String[] multipleParamsTable = currentParam.split(",");
 
	        	// If one PARAM contains more than one value, split the values into an SQL list of values
	        	for(int j = 0; j < multipleParamsTable.length; j++)
	            {
	        		if(j == multipleParamsTable.length - 1)
	        			multipleParams += multipleParamsTable[j];
	        		else
	        			multipleParams += multipleParamsTable[j] + "','";
	            }	            
 
	        	newStatement = newStatement.replaceFirst("PARAM", "'" + multipleParams + "'");
	        }
 
			// Connect to the DB
			java.sql.Connection conn = null;
			java.sql.Statement s = null;
			ResultSet rs = null;
 
			try 
			{
				String dbInfo = AllSettings.myDboconf.getProperty("myDb");
				String[] connectInfo = dbInfo.split(",");
				Class.forName("oracle.jdbc.OracleDriver").newInstance();
				String dbConnectionString = "jdbc:oracle:thin:@" + connectInfo[1] + ":" + connectInfo[2] + ":" + connectInfo[3];				
				conn = DriverManager.getConnection (dbConnectionString, connectInfo[4], connectInfo[5]);
	            s = conn.createStatement();
	            rs = s.executeQuery(newStatement);
	            ResultSetMetaData rsmd = rs.getMetaData();
 
	            // Get the results
	           	while(rs.next())
	           	{
	           		resultList = new JSONObject();
	           		for(int i = 1; i <= rsmd.getColumnCount(); i++)
	           		{
	           			String valueForInsert = rs.getString(i);
	           			if(valueForInsert == null)
	           				valueForInsert = "null"; // Ensures that the values are added to the JSON object even when the value is null
	           			resultList.put(rsmd.getColumnName(i), valueForInsert);
	           		}
	           		statham.put(resultList);
	           	}
 
	        }     
	        catch (Exception e) {
	        	e.printStackTrace();
	        }
	        finally
	        {
	            try {
					s.close();
		           	rs.close();
		        	conn.close(); 
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
 
			// Send back the results JSON
			String callBack = request.getParameter("jsoncallback");
			if(callBack != null)
				out.print(callBack + "(" + statham + ");");
			else
				out.print(statham);
			out.close();
		}
	} | 
Partager