Tool for Json file Conversion.
Get the Data from DB And Make a json file for parsing it in Android or in any applications.
<%@ page import="java.io.*,java.util.*, javax.servlet.*, java.sql.*" %>
<%
Connection con=null;
Statement st=null;
ResultSet rs=null;
String cid=" ";
String cname=" ";
String category=" ";
String image=" ";
String num=" ";
String area=" ";
String video=" ";
String user=" ";
String sql="select * from company"; //query passed in rs=st.executeQuery(sql);
//File creation
String strPath = "F:\\company.json"; // file path to store file at with this extension .json name
File strFile = new File(strPath);
boolean fileCreated = strFile.createNewFile(); // file creation with name and with (.)dot extension json
//File appending
Writer objWriter = new BufferedWriter(new FileWriter(strFile));
objWriter.write("{'employees':["); //json object creation with name employee.
try //getting connection and values from DB. (attributes like address will have problem in json as we will have substring in that, so use xml for that)
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:DB_name");
st=con.createStatement();
rs=st.executeQuery(sql);
while(rs.next()) //while will iterate and print all values in this object
{
cid=rs.getString(1);
cname=rs.getString(2);
category =rs.getString(3);
image =rs.getString(6);
num=rs.getString(9);
area=rs.getString(10);
video=rs.getString(11);
user=rs.getString(14);
objWriter.write("{'cid':'"+cid+"', 'company Name':'"+cname+"', 'category':'"+category+"', 'image':'http://www.xyz.com/"+image+"', 'number':'"+num+"', 'area':'"+area+"', 'video':'http://www.xyz.com/"+video+"', 'User':'"+user+"'},"); // finish it in one line.
}
objWriter.write("]}"); // after iteration close the while and close the employees object
objWriter.flush();
objWriter.close();
out.println("JSON Generated successfully"); //JSON file generated.
}
catch(SQLException e)
{
out.println(e);
}
%>
Great Job!
ReplyDeleteThis is really helpful, Thank You
ReplyDelete