Mysql data into JSON using JSP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suryaaa
    New Member
    • Dec 2016
    • 1

    Mysql data into JSON using JSP

    i am trying to get data from the mysql table in JSON format..

    my jsp code
    Code:
     <%@ page import="java.sql.*" %> <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> <HEAD> <TITLE>Fetching Data From a Database</TITLE> </HEAD> <H1>Fetching Data From a Database</H1> <% 
                 Class.forName("com.mysql.jdbc.Driver");  
     
          Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/tree","root","admin321"); 
         
     
                Statement statement = con.createStatement();
     
                String id = request.getParameter("name");  
     
                ResultSet resultset = 
                   statement.executeQuery("select * from bitab") ; 
     
                if(!resultset.next()) {
                    out.println("Sorry, could not find . ");
                } else {  
            %> <% 
               } 
           %>
           [
     
             <% while (resultset.next()) { %> 
              
      { "name": "<%= resultset.getString("parent") %>" ,   "children": [{ "name": "<%= resultset.getString("child1") %>"},
       
        "children": [{ "name": "<%= resultset.getString("child2") %>"},
       { "name": "<%= resultset.getString("child3") %>"}   ,
         "children": [{ "name": "<%= resultset.getString("child4") %>"}
        ] },
                 ] },
                                 ]},
                                 ]
       
     
     <% } %> ;
    i cant able to get correct json format and also the null values includes as child in json..any suggestions would be helpfull..
    can anyone suggest me a way to get the json as below
    Code:
    {  
       "name":"par1",
       "children":[  
          {  
             "name":"chil 1",
             "children":[  
                {  
                   "name":"chil 11"
                },
                {  
                   "name":"chil 12"
                },
                {  
                   "name":"chil 2",
                   "children":[  
                      {  
                         "name":"chil 21"
                      },
                      {  
                         "name":"chil 22"
                      }
                   ]
                }
             ]
          }
       ]
    }
    Last edited by Frinavale; Jan 3 '17, 09:48 PM.
Working...