001package bradleyross.common;
002//
003//  MySQL1.java
004//  MySQL1
005//
006//  Created by Bradley Ross on Mon Apr 07 2003.
007//  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
008//
009//      For information on setting Java configuration information, including 
010//      setting Java properties, refer to the documentation at
011//              http://developer.apple.com/techpubs/java/java.html
012//
013
014import java.sql.ResultSetMetaData;
015import java.sql.SQLException;
016/**
017* Provide a set of
018* classes to help with the processing of
019* SQL statements.
020*
021*
022* @author Bradley Ross
023*/
024public class sqlHelper
025   {
026   /**
027   * Determines amount of intermediate diagnostic messages to
028   * be printed.
029   * <p>See the description for the setDebugLevel method
030   *    for more information.</p>
031   */
032   protected int debugLevel = 0;
033   /**
034   * Set amount of diagnostic messages to be printed.
035   * 
036   * A value of 0 indicates that no diagnostic messages are
037   * to be printed.  Increased
038   * values increase the quantity of diagnostic messages.
039   * @param level Desired level of diagnostic messages.
040   */
041   public void setDebugLevel (int level)
042      { debugLevel = level; }
043   /**
044   * Print ResultSet generated by SQL query.
045   *<p>This version has the destination for the 
046   *   listing set to System.out</p>
047   * @param rs ResultSet to be printed
048   * @see System
049   */
050   public void printResultSet(java.sql.ResultSet rs)
051      {
052      printResultSet(rs, System.out);
053      }
054   /**
055   * Print ResultSet generated by SQL query.
056   * <p>This version uses a java.io.OutputStream
057   *    so that the results can be sent to a file
058   *    or some other container.</p>
059   * <p>A <code>java.io.OutputSteam</code> object can be
060   *    created using the Java statement
061   *    <code>java.io.FileOutputStream outputFile =
062   *    new java.io.FileOutputStream(outputFileName);
063   *    </code> where <code>outputFileName</code>
064   *    is a <code>String</code> object containing the
065   *    name of the output file to be created.</p>
066   * @param rs ResultSet object to be displayed
067   * @param output Destination for listing
068   * @see java.io.FileOutputStream
069   */
070   public void printResultSet(java.sql.ResultSet rs,
071          java.io.OutputStream output)
072      {
073      try
074         {
075         java.io.PrintStream out =
076            new java.io.PrintStream(output);
077         printResultSet(rs, out);
078         out.flush();
079         }
080      catch (Exception e)
081         {
082         System.out.println(e.getMessage());
083         }
084      }
085   /**
086   * Print ResultSet generated by SQL query.
087   * <p>The object System.out is a java.io.PrintStream
088   *    object.  This version of the method is therefore
089   *    suitable for printing to System.out.</p>
090   * @param rs ResultSet to be printed
091   * @param output Destination for listing
092   * @see System
093   */
094   public void printResultSet(java.sql.ResultSet rs, 
095          java.io.PrintStream output) 
096      {
097      try
098         {
099         if (debugLevel > 0)
100            { output.println("Starting printResultSet"); }
101          //
102          ResultSetMetaData meta = rs.getMetaData();
103          output.print(meta.getColumnCount());
104          output.println(" columns");
105          for (int i=1; i <= meta.getColumnCount(); i++)
106             {
107             output.print(meta.getColumnLabel(i));
108             output.print("   ");
109             output.print(meta.getColumnTypeName(i));
110             output.println("   ");
111             }
112          output.println("***");
113          while (rs.next())
114             {
115             output.println("*** New record");
116             for (int i=1; i <= meta.getColumnCount(); i++)
117                { output.println(rs.getString(i)); }
118             }
119          }
120       catch (SQLException ex1)
121          {
122          output.println("SQLException");
123          ex1.printStackTrace(output);
124          }
125       catch (Exception ex)
126          {
127          output.println("Unknown Exception");
128          ex.printStackTrace(output);
129          }
130   }
131}