print ....

import java.io.IOException;

import java.io.PrintWriter;

import java.io.StringWriter;

import java.util.Vector;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Printtable extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

printTable(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

printTable(request, response);

}

public void printTable(HttpServletRequest request,

HttpServletResponse response) throws IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

HtmlUtils hu = new HtmlUtils();

out.print(hu.createHtmlHeader("Print Table"));

out.print(hu.getTableHead("center", 1));

out.print(hu.getTH("center", "First Name"));

out.print(hu.getTH("center", "Last Name"));

out.print(hu.getTH("center", "Favorite Color"));

out.print(hu.getTH("center", "Gender"));

Vector av = new Vector();

av.addElement("John");

av.addElement("Sample");

av.addElement("Purple");

av.addElement("Male");

av.addElement("Joe");

av.addElement("Bloggs");

av.addElement("Green");

av.addElement("Male");

av.addElement("Fanny");

av.addElement("May");

av.addElement("Blue");

av.addElement("Female");

av.addElement("Joeline");

av.addElement("Bloggs");

av.addElement("Red");

av.addElement("Female");

out.print(hu.getTableContents("center", av, 4));

out.print(hu.getHtmlFooter());

}

}

class HtmlUtils {

public String createHtmlHeader(String title) {

String htmlHeader = null;

htmlHeader = "<HTML><HEAD><TITLE> " + title + " </TITLE></HEAD><BODY>";

return htmlHeader;

}

public String getHtmlFooter() {

String htmlFooter = "</BODY></HTML>";

return htmlFooter;

}

public String getHead(int level, String heading) {

return "<H" + level + "> " + heading + "</H" + level + ">";

}

public String getTableHead(String align, int border) {

String tableHeader = null;

tableHeader = "<TABLE align=" + align + " border=" + border + ">";

return tableHeader;

}

public String getTR(String align) {

String TRCell = null;

TRCell = "<TR align=" + align + ">";

return TRCell;

}

public String getTR() {

String TRCell = null;

TRCell = "<TR>";

return TRCell;

}

public String getTD(String align, String value) {

String TDCell = null;

TDCell = "<TD align=" + align + "> " + value + " </TD>";

return TDCell;

}

public String getTD() {

String TDCell = null;

TDCell = "<TD>";

return TDCell;

}

public String getTD(int width) {

String TDCell = null;

TDCell = "<TD WIDTH=" + width + ">";

return TDCell;

}

public String getTH(String align, String value) {

String THCell = null;

THCell = "<TH align=" + align + "> " + value + " </TH>";

return THCell;

}

public String getTableContents(String align, Vector values,

int elementCounter) throws IOException {

StringWriter Cells = new StringWriter();

String contents = new String();

int vsize = values.size();

Cells.write("<TR>");

for (int i = 0; i < vsize; i++) {

String value = values.elementAt(i).toString();

if (i != 0) {

if (i >= elementCounter) {

if (i % elementCounter == 0) {

Cells.write("</TR>\n\n<TR>");

}

}

}

Cells.write("<TD align=" + align + "> " + value + " </TD> \n");

}

Cells.write("</TR>");

contents = Cells.toString();

Cells.flush();

Cells.close();

return contents;

}

public String getClosedTR() {

String TRCell = null;

TRCell = "</TR>";

return TRCell;

}

public String getClosedTD() {

String TDCell = null;

TDCell = "</TD>";

return TDCell;

}

public String getBR(int lines) {

StringWriter lineBR = new StringWriter();

String lineBRs = new String();

for (int i = 0; i <= lines; i++) {

lineBR.write("
\n");

}

lineBRs = lineBR.toString();

return lineBRs;

}

public String getLI(String item) {

String li = new String("<LI>");

li += item;

return li;

}

}

Above code is printing the mannual entered data ,i want to print the data,those r in data base,how to print the fetched results through print button...plz help me.....

[5145 byte] By [god_bless1_you1a] at [2007-11-27 4:09:30]
# 1
http://java.sun.com/docs/books/tutorial/jdbc/index.html
cotton.ma at 2007-7-12 9:14:57 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
i asking when we connect to data base results will come i want to print those result through print button ,,,,how to write code for that button......
god_bless1_you1a at 2007-7-12 9:14:57 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

You don't. The browser's "print" button simply prints whatever is displayed, and this has nothing to do with your code. If you want it to print "in a certain way" then set up your HTML to display it that way.

Edit: You can, of course, manipulate the HTML (and how it prints) using JavaScript and CSS and other HTML related things. For more information on those, ask in an HTML forum. This has, in any case, nothing to do with JDBC, or even, for that matter, with Java, at all.

masijade.a at 2007-7-12 9:14:57 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...