How to retrieve the data from database and display in pdf format in java
<ol>
<li>I want to display the data which are stored in the database in a pdf file format</li>
<li>I have displayed in web page, instead of that I want to display in pdf. Below is my code, i used a MySQL database</li>
</ol>

<hr>

<pre><code>public class StockReceiptReturnQuery extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String receiptNo = request.getParameter("ReceiptNo");
        int Rno = Integer.parseInt(receiptNo);

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "dinga");
            PreparedStatement ps = con.prepareStatement("select * from project.stockreceiptreturn WHERE ReceiptNO =?");
            ps.setInt(1, Rno);
            out.print("&lt;center&gt;&lt;table width=25% border=1&gt;&lt;/center&gt;");
            out.print("&lt;center&gt;&lt;h1&gt;StockReceiptReturnQuery:&lt;/h1&gt;&lt;/center&gt;");
            ResultSet rs = ps.executeQuery();
            /* Printing column names */
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                out.print("&lt;tr&gt;");
                out.print("&lt;td&gt;" + rsmd.getColumnName(1) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(1) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(2) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(2) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(3) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(3) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(4) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(4) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(5) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(5) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(6) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(6) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(7) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(7) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(8) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(8) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(9) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(9) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(10) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(10) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(11) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(11) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(12) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getDouble(12) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(13) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getDouble(13) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(14) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(14) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(15) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(15) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(16) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getDouble(16) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(17) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getDouble(17) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(18) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getDouble(18) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(19) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getDouble(19) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(20) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(20) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(21) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(21) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(22) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getString(22) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(23) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(23) + "&lt;/td&gt;&lt;/tr&gt;");
                out.print("&lt;tr&gt;&lt;td&gt;" + rsmd.getColumnName(24) + "&lt;/td&gt;");
                out.print("&lt;td&gt;" + rs.getInt(24) + "&lt;/td&gt;&lt;/tr&gt;");

            }
            out.print("&lt;/table&gt;");

        } catch (Exception e2) {
            e2.printStackTrace();
        }

        finally {
            out.close();
        }
    }
}
</code></pre>

<hr>

<ul>
<li>I am getting out put after running pgm</li>
</ul>

<p><img src="https://i.stack.imgur.com/N4NTN.png" alt="enter image description here"></p>