Author Topic: javascript dataModel - URL value ?  (Read 17188 times)

dataSQL_

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 48
    • View Profile
will a JSON LIST populate or only a JSON string ?
« Reply #15 on: December 31, 2016, 08:38:01 pm »
Thank you for all your assistance, I have mapped the servlet and referenced it in the web . xml for the URL within the VIEW.

The data is still not populating:  http://35.166.148.217/faces/index.xhtml

The two beans that deal with the JSON conversion are listed below, any suggestions greatly appreciated.

bean JsonServices . java
Code: [Select]
package com.queryData.services;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;

public class JsonServices {
public static List<JSONObject> getFormattedResult(ResultSet rs) {
// List<JSONObject> resList = new ArrayList<JSONObject>();
List<JSONObject> resList = "{\"data\":" + new ArrayList<JSONObject>() + "}";
// above is the attempt to modify

try {
// get column names
ResultSetMetaData rsMeta = rs.getMetaData();
int columnCnt = rsMeta.getColumnCount();
List<String> columnNames = new ArrayList<String>();
// loop to get all column names
for (int i = 1; i <= columnCnt; i++) {
// adding all retrieved column names to List object
columnNames.add(rsMeta.getColumnName(i).toUpperCase());
}
while (rs.next()) {
// convert each object to an human readable JSON object
JSONObject obj = new JSONObject();
for (int i = 1; i <= columnCnt; i++) {
String key = columnNames.get(i - 1);
String value = rs.getString(i);
obj.put(key, value);
}
resList.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return resList;
}
}


bean Main . java

Code: [Select]
package com.queryData.main;

import com.queryData.dao.DataDAO;
import com.queryData.services.JsonServices;
import java.sql.ResultSet;
import java.util.List;
import org.json.JSONObject;

public class Main {
ResultSet resultSet = null;
DataDAO datadao = new DataDAO();

public List<JSONObject> getJsonObject() {
resultSet = datadao.getResultSet();
List<JSONObject> resList = JsonServices.getFormattedResult(resultSet);
return resList;
}

public static void main(String[] args) {
Main m = new Main();
List<JSONObject> jObj = m.getJsonObject();
for (int i = 0; i < jObj.size(); i++) {
System.out.println(jObj.get(i));
}
}
}

dataSQL_

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 48
    • View Profile
convert to proper array string
« Reply #16 on: January 01, 2017, 06:22:10 pm »
deleted
« Last Edit: January 01, 2017, 08:08:55 pm by dataSQL_ »

dataSQL_

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: javascript dataModel - URL value ?
« Reply #17 on: January 01, 2017, 08:46:20 pm »
Please, I have it ... with one error:

servlet code last line is incorrect and returns error "Void methods cannot return a value"

Please, any suggestions greatly appreciated.
Code: [Select]
    public static void main(String[] args)
    {   
    Main m = new Main();
    List<JSONObject> jObj = m.getJsonObject();
    StringBuilder sb = new StringBuilder();
    for(int i =0 ; i < jObj.size(); i++)
        {
       sb.append(jObj.get(i).toString());
        }   
            {         
            String responseStr = "{\"data\":[" + sb + "]}";
            System.out.println(responseStr);
            return responseStr;
            }               
    }   
}

dataSQL_

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 48
    • View Profile
please delete
« Reply #18 on: January 02, 2017, 01:23:00 am »
Please delete this post.