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 Branislav Lazic · December 15, 2014
Kind of really bad approach. Use classic DAO pattern for database access.

Rough example:

Create POJO like this:


public class Person {

  private int id;
  private String name;
  private int age;
  
  //getters and setters
  
}




DAO interface like this:


import java.util.*;

public interface PersonDAO {

  List<Person> getAll();
  
}




Implementation class of DAO interface:



import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PersonDAOImpl implements PersonDAO{

  @Override
  public List<Person> getAll() {
   
    List<Person> persons = new ArrayList<Person>();
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;

try {
connection = DataBase.DBConn().getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM person");

while (resultSet.next()) {
Person person = new Person();
person.setId(resultSet.getInt("id"));
person.setName(resultSet.getString("name"));
person.setAge(resultSet.getString("age"));

persons.add(person);
}

} catch (Exception e) {

      e.printStackTrace();

    } finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

return persons;
  
  }
  
}


And method should look like this:


@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/v1/json/*")
public Person getAll() {
   return new PersonDAOImpl().getAll();
}



Of course, be careful what JAX-RS implementation you use. Configurations for i.e. Jersey and RESTEasy are different.
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..?
0 Branislav Lazic · December 16, 2014
It's bad i.e. at part where you are passing ResultSet as an argument to a method. And as far as I see, ResultSet remains unclosed. On the other hand, DAO is just a standard "low level" approach (due to its readability) for design of data access layer. 
Also, look at your "returnAllInfo" method. Unnecessarily verbose.
  • 1

Java / Android Development

106,932 followers
About

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

Links
Moderators
Bucky Roberts Administrator