|
How to view database data in PDF format using servlets |
|
|
The example below extracts data from SQL server and outputs it into a web browser in the form of a PDF document using Java Servlets.
import java.io.IOException;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import com.lowagie.text.*;
import java.util.*;
import com.lowagie.text.pdf.PdfWriter;
public class viewDataIntoPDF extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
}
OutputStream out = null;
DB2 db = new DB2();
Connection conn=db.dbConnect(
"jdbc:jtds:sqlserver://localhost:1433/ntconnect","sa","");
Document document = new Document(PageSize.A4, 50, 350, 50, 50);
java.util.List details = db.getDetails(conn);
try {
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
Paragraph paragraph = new Paragraph("View Data");
document.add(paragraph);
ListItem listItem;
com.lowagie.text.List list = new com.lowagie.text.List(true, 15);
Iterator i = details.iterator();
while(i.hasNext()) {
listItem = new ListItem((String)i.next(),
FontFactory.getFont(FontFactory.TIMES_ROMAN, 12));
list.add(listItem);
}
document.add(list);
} catch (Exception e) {
throw new ServletException("Exception in PDF Servlet", e);
} finally {
document.close();
}
document.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
class DB2 {
public DB2() {}
public Connection dbConnect(String db_connect_string,
String db_userid, String db_password) {
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(db_connect_string,
db_userid, db_password);
System.out.println("connected");
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public java.util.List getDetails(Connection conn) {
java.util.List items = null;
String query;
try {
query =
"SELECT col_User, col_Password, col_Date, col_Domain, "+
"col_LastName, col_FirstName, col_Occupation FROM "+
"[tbl_Current-usr] WHERE (col_User LIKE '%JLOGAN%')";
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(query);
items = new ArrayList();
while (rs.next()) {
items.add("Username :: " +rs.getString("col_User"));
items.add("Password :: " +rs.getString("col_password"));
items.add("Date :: " +rs.getString("col_date"));
items.add("Domain :: " +rs.getString("col_domain"));
items.add("Last Name :: " +rs.getString("col_lastname"));
items.add("First Name :: " +rs.getString("col_firstname"));
items.add("Occupation :: " +rs.getString("col_ocupation"));
}
} catch (Exception e) {
e.printStackTrace();
}
return items;
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.