title: How to get database table data(oracle 11g) as an Json object & to display that Json object in Android?
body: <p>I am trying to create a connection between android app and oracle DB. I found that I have to create a web service, so I started writing a servlet which would crate a connection with database and return the data as JSON object which can be parsed on android.<br>
  To start with I wrote a servlet to return data in HTML format which was pretty easy, following is my servlet code</p>
  
  <pre><code>package a.b.c;
  
  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 ServletDatabaseConnect extends HttpServlet  
  {
      protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
      {
          PrintWriter pw=res.getWriter();
          res.setContentType("text/html");        
          String tb=req.getParameter("table");    
  
          try
          {
               Class.forName("oracle.jdbc.driver.OracleDriver");
               Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","admin");
               Statement st=con.createStatement();
               System.out.println("connection established successfully...!!");     
  
               ResultSet rs=st.executeQuery("Select * from "+tb);
  
               pw.println("&lt;table border=1&gt;");
                   while(rs.next())
                   {
                       pw.println("&lt;tr&gt;&lt;td&gt;"+rs.getInt(1)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(2)+"&lt;/td&gt;"+
                                        "&lt;td&gt;"+rs.getString(3)+"&lt;/td&gt;&lt;/tr&gt;");
                   }
               pw.println("&lt;/table&gt;");
               pw.close();
          }
          catch (Exception e){
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>How do I modify my servlet code to return the data in JSON object format?   </p>