Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
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;
	}
}



This is my JSON class for processing DataBase records.

Java
@Path("/v1/inventory")
public class Inventory {
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response returnAllInfo() throws Exception {
		Connection conn = null;
		PreparedStatement query = null;
		String returnString = null;
		Response rb = 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();
			rb = Response.ok(returnString).build();

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

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



this is the class for populating that response to client(via URI on browser)

(localhost:7001/My_pkg/api/v1/inventory) it gives a blank page and no error while if i do another path hit.
(localhost:7001/My_pkg/api/v1/status) it gives me record from DataBase :(

Kindly help what is wrong in it.?


Some points :
I am using SQL Server as my DB source.
Oracle WebLogic Server(12.1.3) for Web-Service deployment.
Posted
Updated 12-Dec-14 6:52am
v3
Comments
ZurdoDev 12-Dec-14 12:35pm    
You'll need to debug the code to see what is happening.
[no name] 12-Dec-14 12:49pm    
Thank you for your very quick response :/ but i am stuck at it, also tried Debugging will try that more precisely , did you find any problem in code .?

1 solution

I solved it by changing the Web.xml <param-value> to my current package name..! ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900