     //----------------------------------------------------------------------------
     //
     // Module:      SimpleSelect.java
     //
     // Description: Program for ODBC API interface. 
     //                       This java application will:
     //                           1.- Connect to a JDBC driver, 
     //                           2.- Issue a select statement
     //                           3.- Display all result columns and rows
     //
     // Product:      JDBC to ODBC Bridge
     // Author:        Rene Astudillo Muņoz
     //
     // Date:            April, 1999
     //
     // Copyright:   1998-1996 Riga Linz, S.A.
     // This software contains confidential and proprietary
     // information of Riga Linz, S.A.
     //----------------------------------------------------------------------------


import java.net.URL;
import java.sql.*;

class SimpleSelect {


   public static void main (String args[]) {
      Statement stmt, pstmt;
      PreparedStatement pstmt2;
      ResultSet rs;
      String url   = "jdbc:odbc:DSNAlumnos";
      String query = "SELECT ramo,apellido,nombre,s1,s2,s3,s4,s5,s6 FROM alumnos WHERE ramo LIKE 'SO3LUMA%' ORDER BY ramo,apellido,nombre"; 
      int nCommand =0;
      int nRet = 0;

      nCommand = 0;
      try {
         // Load the jdbc-odbc bridge driver
         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

         //DriverManager.setLogStream(System.out);

         // Attempt to connect to a driver.  
         // Each one of the registered drivers will be loaded until
         // one is found that can process this URL
         Connection con = DriverManager.getConnection ( url, "", "");

         // If we were unable to connect, an exception would have been thrown.  
         // So, if we get here, we are successfully connected to the URL
         // Check for, and display and warnings generated by the connect.
         checkForWarning (con.getWarnings ());

         // Get the DatabaseMetaData object and display
         // some information about the connection
         DatabaseMetaData dma = con.getMetaData ();

         System.out.println("Connected to " + dma.getURL());
         System.out.println("Driver       " + dma.getDriverName());
         System.out.println("Version      " + dma.getDriverVersion());
         System.out.println("");

         switch(nCommand){
            case 0:
               stmt = con.createStatement ();
               rs = stmt.executeQuery (query);
               dispResultSet (rs);
               rs.close();
               stmt.close();
               break;
         }
         // Close the connection
         con.close();
      }catch (SQLException ex) {

         // A SQLException was generated.  Catch it and display the error information.  
         // Note that there could be multiple error objects chained together
         System.out.println ("\n*** SQLException caught ***\n");
         while (ex != null) {
            System.out.println ("SQLState: " + ex.getSQLState ());
            System.out.println ("Message:  " + ex.getMessage ());
            System.out.println ("Vendor:   " + ex.getErrorCode ());
            ex = ex.getNextException ();
            System.out.println ("");
          }
       }catch (java.lang.Exception ex) {
          // Got some other type of exception.  Dump it.
          ex.printStackTrace ();
       }
   }

   //-------------------------------------------------------------------
   // checkForWarning
   // Checks for and displays warnings.  Returns true if a warning existed
   //-------------------------------------------------------------------

   private static boolean checkForWarning (SQLWarning warn) throws SQLException  {
      boolean rc = false;

      // If a SQLWarning object was given, display the warning messages.  
      // Note that there could be multiple warnings chained together
      if (warn != null) {
         System.out.println ("\n *** Warning ***\n");
         rc = true;
         while (warn != null) {
            System.out.println ("SQLState: " + warn.getSQLState ());
            System.out.println ("Message:  " + warn.getMessage ());
            System.out.println ("Vendor:   " + warn.getErrorCode ());
            System.out.println ("");
            warn = warn.getNextWarning ();
         }
      }
      return rc;
     }

     //-------------------------------------------------------------------
     // dispResultSet
     // Displays all columns and rows in the given result set
     //-------------------------------------------------------------------

     private static void dispResultSet (ResultSet rs) throws SQLException
     {
         int i;

         // Get the ResultSetMetaData.  This will be used for the column headings
         ResultSetMetaData rsmd = rs.getMetaData ();

         // Get the number of columns in the result set
         int numCols = rsmd.getColumnCount ();

	System.out.print("<HTML><BODY><CENTER><TABLE>");
	System.out.println("");
         // Display column headings
         for (i=1; i<=numCols; i++) {
                     System.out.print("<TH>");
                     System.out.print(rsmd.getColumnLabel(i));
                     System.out.print("</TH>");
         }
         System.out.println("");
             
         // Display data, fetching until end of the result set
         boolean more = rs.next ();
         while (more) {
            // Loop through each column, getting the column data and displaying
            System.out.print("<TR>");
            for (i=1; i<=numCols; i++) {
               System.out.print("<TD>");
               System.out.print(rs.getString(i));
               System.out.print("</TD>");
            }
            System.out.print("</TR>");
            System.out.println("");

            // Fetch the next result set row
            more = rs.next ();
         }
         System.out.println("</CENTER></TABLE></BODY></HTML>");
   }
}
