home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDBC / SimpleSelect.java < prev   
Encoding:
Java Source  |  2000-05-04  |  4.5 KB  |  187 lines

  1. // 
  2. //----------------------------------------------------------------------------
  3. //
  4. // This is a sample file that shows how to connect to a ODBC data source.
  5. // Please make sure you have a valid data source URL in your url variable.
  6. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved. 
  7. //
  8. //----------------------------------------------------------------------------
  9.  
  10.  
  11. import java.sql.*;
  12.  
  13. class SimpleSelect {
  14.  
  15.  
  16. public static void main (String args[]) {
  17.     String url   = "JDBC:ODBC:my_dsn";
  18.     String query = "SELECT * FROM authors"; 
  19.  
  20.     try {
  21.  
  22.         // Load the jdbc-odbc bridge driver
  23.  
  24.         Class.forName ("com.ms.jdbc.odbc.JdbcOdbcDriver");
  25.  
  26.         DriverManager.setLogStream(System.out);
  27.  
  28.         // Attempt to connect to a driver.  
  29.  
  30.         Connection con = DriverManager.getConnection (
  31.             url, "my_user", "my_pwd");
  32.  
  33.         // If we were unable to connect, an exception
  34.         // would have been thrown.  So, if we get here,
  35.         // we are successfully connected to the URL
  36.  
  37.         // Check for, and display and warnings generated
  38.         // by the connect.
  39.  
  40.         checkForWarning (con.getWarnings ());
  41.  
  42.         // Get the DatabaseMetaData object and display
  43.         // some information about the connection
  44.  
  45.         DatabaseMetaData dma = con.getMetaData ();
  46.  
  47.         System.out.println("\nConnected to " + dma.getURL());
  48.         System.out.println("Driver       " + 
  49.             dma.getDriverName());
  50.         System.out.println("Version      " +
  51.             dma.getDriverVersion());
  52.         System.out.println("");
  53.  
  54.         // Create a Statement object so we can submit
  55.         // SQL statements to the driver
  56.  
  57.         Statement stmt = con.createStatement ();
  58.  
  59.         // Submit a query, creating a ResultSet object
  60.  
  61.         ResultSet rs = stmt.executeQuery (query);
  62.  
  63.         // Display all columns and rows from the result set
  64.  
  65.         dispResultSet (rs);
  66.  
  67.         // Close the result set
  68.  
  69.         rs.close();
  70.  
  71.         // Close the statement
  72.  
  73.         stmt.close();
  74.  
  75.         // Close the connection
  76.  
  77.         con.close();
  78.     }
  79.  
  80.     catch (SQLException ex) {
  81.  
  82.         // A SQLException was generated.  Catch it and
  83.         // display the error information.  Note that there
  84.         // could be multiple error objects chained
  85.         // together
  86.  
  87.     System.out.println ("\n*** SQLException caught ***\n");
  88.  
  89.     while (ex != null) {
  90.         System.out.println ("SQLState: " +
  91.                 ex.getSQLState ());
  92.         System.out.println ("Message:  " + ex.getMessage ());
  93.         System.out.println ("Vendor:   " +
  94.                 ex.getErrorCode ());
  95.         ex = ex.getNextException ();
  96.         System.out.println ("");
  97.         }
  98.     }
  99.     catch (java.lang.Exception ex) {
  100.  
  101.         // Got some other type of exception.  Dump it.
  102.  
  103.         ex.printStackTrace ();
  104.     }
  105. }
  106.  
  107. //-------------------------------------------------------------------
  108. // checkForWarning
  109. // Checks for and displays warnings.  Returns true if a warning
  110. // existed
  111. //-------------------------------------------------------------------
  112.  
  113. private static boolean checkForWarning (SQLWarning warn)     
  114.                                         throws SQLException  {
  115.     boolean rc = false;
  116.  
  117.     // If a SQLWarning object was given, display the
  118.     // warning messages.  Note that there could be
  119.     // multiple warnings chained together
  120.  
  121.     if (warn != null) {
  122.         System.out.println ("\n *** Warning ***\n");
  123.         rc = true;
  124.         while (warn != null) {
  125.             System.out.println ("SQLState: " +
  126.                 warn.getSQLState ());
  127.             System.out.println ("Message:  " +
  128.                 warn.getMessage ());
  129.             System.out.println ("Vendor:   " +
  130.                 warn.getErrorCode ());
  131.             System.out.println ("");
  132.             warn = warn.getNextWarning ();
  133.         }
  134.     }
  135.     return rc;
  136. }
  137.  
  138. //-------------------------------------------------------------------
  139. // dispResultSet
  140. // Displays all columns and rows in the given result set
  141. //-------------------------------------------------------------------
  142.  
  143. private static void dispResultSet (ResultSet rs)
  144.     throws SQLException
  145. {
  146.     int i;
  147.  
  148.     // Get the ResultSetMetaData.  This will be used for
  149.     // the column headings
  150.  
  151.     ResultSetMetaData rsmd = rs.getMetaData ();
  152.  
  153.     // Get the number of columns in the result set
  154.  
  155.     int numCols = rsmd.getColumnCount ();
  156.  
  157.     // Display column headings
  158.  
  159.     for (i=1; i<=numCols; i++) {
  160.         if (i > 1) System.out.print(",");
  161.         System.out.print(rsmd.getColumnLabel(i));
  162.     }
  163.     System.out.println("");
  164.     
  165.     // Display data, fetching until end of the result set
  166.  
  167.     boolean more = rs.next ();
  168.     while (more) {
  169.  
  170.         // Loop through each column, getting the
  171.         // column data and displaying
  172.  
  173.         for (i=1; i<=numCols; i++) {
  174.             if (i > 1) System.out.print(",");
  175.             System.out.print(rs.getString(i));
  176.         }
  177.         System.out.println("");
  178.  
  179.         // Fetch the next result set row
  180.  
  181.         more = rs.next ();
  182.     }
  183. }
  184.  
  185. }
  186.  
  187.