JSON response not showing on URI

0 Muhammad Faisal Hyder · December 12, 2014
Hye all , i am making a Java Restful WebService.!
i make hit on DataBase and it populates data from Path in URI but when i do JSON response hit it doesn't populate data from DataBase , i dont know what is wrong it should wotk it is totally insane that no error and still not working :/ Kindly Help ....



My class for Retreiving Json response.


package com.faisal.JSON;

import java.sql.ResultSet;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

public class JSONClass {

public JSONArray JsonArray(ResultSet rs) throws Exception {
JSONArray JsonArray = new JSONArray();
try {
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
       int numColumn = rsmd.getColumnCount();
JSONObject jsonObject = new JSONObject();
       for (int i = 1; i < numColumn + 1; i++) {
String column_name = rsmd.getColumnName(i);
if (rsmd.getColumnType(i) == java.sql.Types.ARRAY) { jsonObject.put(column_name, rs.getArray(column_name));
System.out.println("JSONClass:ARRAY"); }

      else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
jsonObject.put(column_name, rs.getNString(column_name));
System.out.println("JSONClass:VARCHAR");
}
     
      else if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
jsonObject.put(column_name, rs.getInt(i));
System.out.println("JSONClass:INTEGER");
}

      else if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
jsonObject.put(column_name,rs.getTimestamp(column_name));
System.out.println("JSONClass:TIMESTAMP");
       }

else if (rsmd.getColumnType(i) == java.sql.Types.NUMERIC) {
jsonObject.put(column_name,rs.getBigDecimal(column_name));
}

       else if (rsmd.getColumnType(i) == java.sql.Types.BIGINT) {
jsonObject.put(column_name, rs.getInt(column_name));
}
 
        else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) {
jsonObject.put(column_name, rs.getBlob(column_name));
}

       else if (rsmd.getColumnType(i) == java.sql.Types.BOOLEAN) {
       jsonObject.put(column_name, rs.getBoolean(column_name));
}


       else if (rsmd.getColumnType(i) == java.sql.Types.DATE) {
jsonObject.put(column_name, rs.getDate(column_name));
}

       else if (rsmd.getColumnType(i) == java.sql.Types.FLOAT) {
jsonObject.put(column_name, rs.getFloat(column_name));
}

        else if (rsmd.getColumnType(i) == java.sql.Types.DOUBLE) {
jsonObject.put(column_name, rs.getDouble(column_name));
}

        else if (rsmd.getColumnType(i) == java.sql.Types.SMALLINT) {
jsonObject.put(column_name, rs.getInt(column_name));
}

      else {
jsonObject.put(column_name, rs.getObject(column_name));
System.out.println("JSONClass:Object" + column_name);
}

}// for end
JsonArray.put(jsonObject);
} // while end

}
catch (Exception e) {
e.printStackTrace();
}
return JsonArray;
}
}

Now class for Populating it to URI.

package com.faisal.restinventory;


@Path("/v1/json/*")
public class Inventory {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String returnAllInfo() throws Exception {
Connection conn = null;
PreparedStatement query = null;
String returnString = null;
try {
conn = DataBase.DBConn().getConnection();
query = conn.prepareStatement("select * from WS");
ResultSet rs = query.executeQuery();

JSONClass JsonClassObject = new JSONClass();
JSONArray GetJsonArray = new JSONArray();

GetJsonArray = JsonClassObject.JsonArray(rs);
query.close();
returnString = GetJsonArray.toString();

} catch (Exception e) {
e.printStackTrace();
}

finally {
if (conn != null)
conn.close();
}
return returnString;
}
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Muhammad Faisal Hyder · December 16, 2014
Thank you for your guidance and quite clean way of telling,
kindly tell why (my) this approach is bad .? is it not safe or some kind of Old approach..?
  • 1

Java / Android Development

130,022 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator