/* * @(#)DataSourceList * * Copyright (c) 1999 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author Karl Moss * @version 1.0 * @date 17Feb99 * */ package javaservlets.nativeCode; import javax.servlet.*; import javax.servlet.http.*; /** * This servlet uses native code to gather a list of the * current ODBC data sources. */ public class DataSourceList extends HttpServlet { /** *

Performs the HTTP GET operation * * @param req The request from the client * @param resp The response from the servlet */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Set the content type of the response resp.setContentType("text/html"); // Get the PrintWriter to write the response java.io.PrintWriter out = resp.getWriter(); // Create the header out.println(""); out.println(""); out.println("ODBC Data Source List"); out.println(""); out.println("

"); out.println("

ODBC Data Sources Available To Servlets " + "Running in the Current Servlet Engine

"); out.println("
"); out.println(""); out.println(""); out.println(""); int henv = 0; try { // Allocate an environment handle henv = JavaToODBC.SQLAllocEnv(); } catch (Exception ex) { out.println("ERROR: Unable to allocate environment"); } // Loop through the data sources until the end of file // is reached while (true) { // Create a new DataSource object to hold the // data source attributes (name, description) DataSource ds = new DataSource(); boolean b = false; try { // Make a native ODBC call to get the next data source // entry. The first call will return the first data source; // any subsequent calls will return the next data source b = JavaToODBC.SQLDataSources(henv, ds); } catch (Exception ex) { ex.printStackTrace(); out.println("
Data Source NameDescription

ERROR: " + ex.getMessage()); break; } // SQLDataSources returns false if there are no more // data sources if (!b) { break; } // Add this data source to the table out.println("" + ds.getName() + "" + ds.getDescription() + ""); } if (henv != 0) { try { // Free the environment handle JavaToODBC.SQLFreeEnv(henv); } catch (Exception ex) { // Ignore any errors } } // Wrap up out.println("
"); out.println(""); out.println(""); out.flush(); } /** *

Initialize the servlet. This is called once when the * servlet is loaded. It is guaranteed to complete before any * requests are made to the servlet * * @param cfg Servlet configuration information */ public void init(ServletConfig cfg) throws ServletException { super.init(cfg); } /** *

Destroy the servlet. This is called once when the servlet * is unloaded. */ public void destroy() { super.destroy(); } }