Ejemplo de una aplicación JDBC simple

Una aplicación JDBC simple muestra los elementos básicos que tienen que incluir las aplicaciones JDBC.

Figura 1. Aplicación JDBC sencilla
import java.sql.*;                                                            1 

public class EzJava 
{
  public static void main(String[] args) 
  {
    String urlPrefix = "jdbc:db2:";
    String url;
    String user;
    String password;
    String empNo;                                                             2 
    Connection con;
    Statement stmt;
    ResultSet rs;
    
    System.out.println ("**** Enter class EzJava");
    
    // Check the that first argument has the correct form for the portion
    // of the URL that follows jdbc:db2:,
    // as described
    // in the Connecting to a data source using the DriverManager 
    // interface with the IBM Data Server Driver for JDBC and SQLJ topic.
    // For example, for IBM Data Server Driver for 
    // JDBC and SQLJ type 2 connectivity, 
    // args[0] might be MVS1DB2M. For 
    // type 4 connectivity, args[0] might
    // be //stlmvs1:10110/MVS1DB2M.


    if (args.length!=3)
    {
      System.err.println ("Invalid value. First argument appended to "+
       "jdbc:db2: must specify a valid URL.");
      System.err.println ("Second argument must be a valid user ID.");
      System.err.println ("Third argument must be the password for the user ID.");
      System.exit(1);
    }
    url = urlPrefix + args[0];
    user = args[1];
    password = args[2];
    try 
    {                                                                        
      // Load the driver
      Class.forName("com.ibm.db2.jcc.DB2Driver");                             3a 
      System.out.println("**** Loaded the JDBC driver");

      // Create the connection using the IBM Data Server Driver for JDBC and SQLJ
      con = DriverManager.getConnection (url, user, password);                3b 
      // Commit changes manually
      con.setAutoCommit(false);
      System.out.println("**** Created a JDBC connection to the data source");

      // Create the Statement
      stmt = con.createStatement();                                           4a 
      System.out.println("**** Created JDBC Statement object");

      // Execute a query and generate a ResultSet instance
      rs = stmt.executeQuery("SELECT EMPNO FROM EMPLOYEE");                   4b 
      System.out.println("**** Created JDBC ResultSet object");

      // Print all of the employee numbers to standard output device
      while (rs.next()) {
        empNo = rs.getString(1);
        System.out.println("Employee number = " + empNo);
      }
      System.out.println("**** Fetched all rows from JDBC ResultSet");
      // Close the ResultSet
      rs.close();
      System.out.println("**** Closed JDBC ResultSet");
      
      // Close the Statement
      stmt.close();
      System.out.println("**** Closed JDBC Statement");

      // Connection must be on a unit-of-work boundary to allow close
      con.commit();
      System.out.println ( "**** Transaction committed" );
      
      // Close the connection
      con.close();                                                            6 
      System.out.println("**** Disconnected from data source");

      System.out.println("**** JDBC Exit from class EzJava - no errors");

    }
    
    catch (ClassNotFoundException e)
    {
      System.err.println("Could not load JDBC driver");
      System.out.println("Exception: " + e);
      e.printStackTrace();
    }

    catch(SQLException ex)                                                    5  
    {
      System.err.println("SQLException information");
      while(ex!=null) {
        System.err.println ("Error msg: " + ex.getMessage());
        System.err.println ("SQLSTATE: " + ex.getSQLState());
        System.err.println ("Error code: " + ex.getErrorCode());
        ex.printStackTrace();
        ex = ex.getNextException(); // For drivers that support chained exceptions
      }
    }
  }  // End main
}    // End EzJava

Notas a la Figura 1 :

Nota Descripción
1 Esta sentencia importa el paquete java.sql, el cual contiene la API básica de JDBC. Para obtener información sobre otros paquetes de Java a los que puede que necesite acceder, consulte «Paquetes de Java para asistencia de JDBC ».
2 La variable empNo de tipo String realiza la función de una variable del lenguaje principal. Es decir, se utiliza para contener datos obtenidos en una consulta de SQL. Consulte "Variables en aplicaciones JDBC" para obtener más información.
3a y 3b Estos dos conjuntos de sentencias muestran cómo conectar con una fuente de datos utilizando una de dos interfaces disponibles. Consulte "Cómo las aplicaciones JDBC conectan con una fuente de datos" para conocer más detalles.

El paso 3a (carga del controlador JDBC) no es necesario si se utiliza JDBC 4.0 o posterior.

4a y 4b Estos dos conjuntos de sentencias muestran cómo ejecutar una operación SELECT en JDBC. Para obtener información sobre cómo realizar otras operaciones de SQL, consulte "Interfaces de JDBC para ejecutar SQL".
5 Este bloque try/catch muestra el uso de la clase SQLException para el manejo de errores de SQL. Para obtener más información sobre cómo manejar errores SQL, consulte «Handling an SQLException under the IBM® Data Server Driver for JDBC and SQLJ ». Para obtener información sobre cómo manejar las advertencias SQL, consulte «Handling an SQLWarning under the IBM Data Server Driver for JDBC and SQLJ ».
6 Esta sentencia desconecta la aplicación respecto de la fuente de datos. Consulte "Desconexión de fuente de datos en aplicaciones JDBC".