Thursday, February 19, 2015

Producing a report using JSP and java

First import the required file to the jsp file.

<%@page import="pac.LoginServlet"%>
<%@page import="pac.SaveRegistrationsServlet"%>
<%@page import="pac.DatabaseConnection"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
As we are writing the java code in a JSP file we are using the <% %> tags to write the java code.

Next we write a java code to write the SQL query.

<% 
        String id=LoginServlet.id;
        out.print(id);
        DatabaseConnection db=new DatabaseConnection();
        String sql="SELECT * from members where m_id ='"+id+"'";
        Statement st=db.getConnection().createStatement();
        ResultSet rs=st.executeQuery(sql);
        while(rs.next()){
            out.print(rs.getString(2));
        %>


To create the database connection class read this: Database Connection




DatabaseConnection is the java class that contains the database connection.
In the string we have given the SQL query.
And then using the statement and the resutset we send the quey and receive the data to the resultset.
<% %> tags are used to write the java coding

<div class="member_card" align="center" >
<table width="50%"  border="2" bordercolor="#000000">
  <tr>
    <th colspan="3" scope="col"><p align="center"><img src="images/nsbm.gif" width="204" height="89"></p>
      <p align="center">MAIN HEADING</p>
      <p align="center">&nbsp; </p>
      <p>
      <div class="data" align="center">
          <table border="0" width="50%">
   
    <tr>
<th>FIRST NAME:</th>
<td><%=rs.getString(1)  %></td>
    </tr>
    <tr></tr>
   
    <tr>
<th>LAST NAME:</th>
<td><%=rs.getString(2)  %></td>
    </tr>
    <tr></tr>
   
    <tr>
<th>GENDER:</th>
<td><%=rs.getString(3)  %></td>
    </tr>
    <tr></tr>
   
    <tr>
<th>ADDRESS:</th>
<td><%=rs.getString(6)  %></td>
    </tr>
    <tr></tr>
   
    <tr>
<th>TELEPHONE:</th>
<td><%=rs.getString(7)  %></td>
    </tr>
    <tr></tr>
    
    <tr>
<th>COUNTRY:</th>
<td><%=rs.getString(10)  %></td>
    </tr>
    <tr></tr>
</table>
      </div>
      </p>
      <p align="center">&footer;</p>
      <p>footer</p>
      <p>footer </p>
      <p>footer </p>
      </th>
  </tr>
</table>
</div>

Here I have used the basic information of a person for the report.

The <%=rs.getString(7)  %> tags is used to get the data from the sql server. As we are taking the data from a table the column number is mentioned inside brackets.

No comments:

Post a Comment