aspose file tools*
The moose likes Servlets and the fly likes Generate JSON File from database data using servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Generate JSON File from database data using servlet" Watch "Generate JSON File from database data using servlet" New topic
Author

Generate JSON File from database data using servlet

ramu yadiki
Greenhorn

Joined: Jul 26, 2013
Posts: 13

hi friends please tell me how to create these type in JSON with Servlet.
in my database table i have one table with two columns name and value with some data. so, i want to create that databse data into one json file using servlet. the JSON file data will be like below things. please tell me how to create these json file using servlet


{
"data" : [
{
"name" : "Unit_1",
"value" : 20

},
{
"name" : "Unit_2",
"value" : 10
},
{
"name" : "Unit_3",
"value" : 30
},
{
"name" : "Unit_4",
"value" : 10
},
{
"name" : "Unit_5",
"value" : 29
}
]
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

There is no API in the servlet specification for generating JSON. You should use one of the available libraries such as Gson or Jackson.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
If you are lucky enough to be using EE7 then you can use spec APIs http://docs.oracle.com/javaee/7/tutorial/doc/jsonp.htm but I suspect you are actually looking for something else according to http://www.coderanch.com/t/623082/Servlets/java/Create-Dynamic-JSON-Servlet
ramu yadiki
Greenhorn

Joined: Jul 26, 2013
Posts: 13

yes i want that related. because i'm new for all technologies that's why i don't know the functionality and how to write that things inside servlet
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Then pick a package and read through the tutorials. You're not going to get anywhere by just throwing your hands up in the air.

Let's take Gson -- here's its User's Guide. Start reading.
ramu yadiki
Greenhorn

Joined: Jul 26, 2013
Posts: 13

i tried these code. here data is comming from database, so next i want to create these data into json format. can you tell me anyone
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("jdbcracle: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);
}
}
}
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
If you read through the responses you have been getting you will find advice to put the data into a POJO and then read a tutorial for a JSON API (you get to choose one) which will show you how to create the JSON (typically in a one line statement).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Have you read the Gson user's guide?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Generate JSON File from database data using servlet