title: java simple json fetch data from database and display
body: <p>i would like to ask on how to produce the desire json output like below:</p>
  
  <pre><code>{
   "Result":"OK",
   "Records":[
    {"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"},
    {"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"},
    {"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"},
    {"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"}
   ]
  }
  </code></pre>
  
  <p>my jsp code are look like below:</p>
  
  <pre><code>&lt;%@page language="java" import="java.sql.*"%&gt;
  &lt;%@page import="java.util.*" %&gt;
  &lt;%@page contentType="text/html; charset=UTF-8"%&gt;
  &lt;%@page import="org.json.simple.JSONArray"%&gt;
  &lt;%@page import="org.json.simple.JSONObject"%&gt;
  &lt;%@page import="org.json.simple.parser.JSONParser"%&gt;
  &lt;%@page import="org.json.simple.parser.ParseException"%&gt;
  &lt;%
      String dept = (String)request.getParameter("dept");
      String sql  = "SELECT * FROM employees WHERE department='"+dept+"'";
  
      try
      {
          Class.forName("com.mysql.jdbc.Driver");
          Connection conn=null;
          conn=DriverManager.getConnection("jdbc:mysql://localhost/jspjsons","root","123456");
          ResultSet rs=null;
          Statement stm1=conn.createStatement();
  
          JSONArray list = new JSONArray();
          rs=stm1.executeQuery(sql);
          while(rs.next())
          {
              JSONObject obj=new JSONObject();
              obj.put("PersonId", rs.getString("id"));
              obj.put("Name", rs.getString("name"));
              obj.put("Age", rs.getString("age"));
              obj.put("RecordDate", rs.getString("date"));
  
              list.add(obj);
          }
  
          out.print(list);
      }
      catch(Exception ex)
      {
          out.println("&lt;h1&gt;"+ex+"&lt;/g1&gt;");
      }
  %&gt;
  </code></pre>
  
  <p>also upon display the output there is always the front and back bracket like this [], how do i get rid of it? need it to start and end with {} not []</p>