/* * DatabaseServlet.java * * Copyright (c) 1998 Hubert A. Klein Ikkink, aka Mr.Haki * * PLEASE DO NOT MODIFY THIS SOURCE FILE WITHOUT CHANGING * COPYRIGHT AND AUTHOR NOTICES. THANKS. * * HUBERT A. KLEIN IKKINK SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. * */ package com.drbob42.article.dbservlet; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.beans.*; import borland.jbcl.dataset.*; import borland.sql.dataset.*; import borland.jbcl.util.Diagnostic; /** * Servlet with database connection. This servlet will show * results from a table to the user in a HTML table. * * @author Hubert A. Klein Ikkink * @version 1.0, December 03, 1998 */ public class DatabaseServlet extends HttpServlet { DatabaseServletDataModule databaseServletDataModule1; /** * Initialize global variables. * Set up the database connection, by intializing the data module. */ public void init(ServletConfig config) throws ServletException { super.init(config); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } /** * Process the HTTP Post request. * Execute the query on the table, get result and put them * in a HTML table. */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = new PrintWriter (response.getOutputStream()); out.println(""); out.println("DatabaseServlet"); /* * Store queryDataSet in data module in a local variable * for shorter name usage */ QueryDataSet dataSet = databaseServletDataModule1.getQueryDataSet1(); try { /* * Execute the query */ dataSet.executeQuery(); /* * Show name of the table */ out.println("

" + dataSet.getTableName() + "

"); out.flush(); /* * Store columns objects of the dataset in an array */ Column[] columns = dataSet.getColumns(); /* * Send column names to the browser */ out.println(""); for (int i = 0; i < columns.length; i++) { /* * Get caption of the column and put it in a HTML table header cell */ out.print(""); } out.println(""); /* * Loop through results, until no more records available */ while (dataSet.next()) { out.print(""); /* * Get value for column by looping through all * availble columns in a record */ for (int i = 0; i < columns.length; i++) { /* * Get value of record cell, formatted as a String */ out.print(""); } out.println(""); } out.println("
" + columns[i].getCaption() + "
" + dataSet.format(i) + "
"); out.flush(); /* * Close dataset again */ dataSet.close(); } catch (DataSetException dsex) { dsex.printStackTrace(); } finally { if (dataSet != null) { try { dataSet.close(); } catch (DataSetException dsex) { /* do nothing */ } } } out.println(""); out.println(""); out.flush(); out.close(); } /** * Get Servlet information */ public String getServletInfo() { return "com.drbob42.article.dbservlet.DatabaseServlet Information"; } private void jbInit() throws Exception { databaseServletDataModule1 = new com.drbob42.article.dbservlet.DatabaseServletDataModule(); } }