0

This is my servlet, I successfully read the data from database and displayed it in the browser but I want to display this data in json format can any one tell me

package com.file;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetDataTable extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("application/json");
PrintWriter pw = res.getWriter();
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:java","java","java");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from SERIESCHART");

pw.println("DATA "+ "VALUE" + "<br>");
while(rs.next())
{
pw.println(rs.getString(1)+" "+rs.getString(2) + "<br>");
}
}
catch (Exception e){
pw.println(e);
}
}
}
2
  • You want to export JSON but it looks like you are writing HTML Commented Nov 6, 2013 at 7:04
  • no i want these json data into extjs grid. here i connect with db and taking that data only.but how i get these data into json after that how i pass these into extjs Commented Nov 6, 2013 at 7:25

2 Answers 2

1

You can use json-simple

sample code

      import org.json.simple.JSONObject;
      JSONObject obj = new JSONObject();

      obj.put("name", "foo");
      obj.put("num",rs.getInt(1));
      obj.put("balance", rs.getString(2));
      obj.put("is_vip", rs.getString(3));

      System.out.print(obj);
Sign up to request clarification or add additional context in comments.

3 Comments

here obj.put("name", "foo"); obj.put("num", new Integer(100)); obj.put("balance", new Double(1000.21)); obj.put("is_vip", new Boolean(true)); you pass static data. but i have taken data dynamically from database. so, i want to pass that db data into json file.
Personally I'm a fan of GSON, it has the extra weight of needing a class definition to serialize, but I don't mind the extra typing/classes.
You can find a lot of tutorials and documentation check this code.google.com/p/json-simple
0

You can do something like this:

...
JsonWriter writer = new JsonWriter(new OutputStreamWriter(res.getOutputStream(), "UTF-8"));
writer.beginArray();
while(rs.next())
{
writer.beginObject();
writer.name("DATA").value(rs.getString(1));
writer.name("VALUE").value(rs.getString(2));
writer.endObject();
}
writer.endArray();
writer.close();
....

This will fetch your results from db and put the json representation into responce outputstream

2 Comments

when ever i put this code the output data will want to store some location of the system. but actually these data i want to put in extjs charts
as you can see that JsonWriter is created of response output stream. you can change that to whatever suites you best

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.