FileDocCategorySizeDatePackage
ExecuteDefinedSelect.javaAPI DocExample2360Thu Jun 14 17:46:54 BST 2001None

ExecuteDefinedSelect

public class ExecuteDefinedSelect extends Object

Fields Summary
Connection
conn
Constructors Summary
public ExecuteDefinedSelect()

    try {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      conn = DriverManager.getConnection(
       "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
    }
  
Methods Summary
public voidexecuteDefinedSelect(java.lang.String sql)

    Date       inactive_date = null;
    DateFormat df            = 
     DateFormat.getDateInstance(DateFormat.SHORT);
    int        rows          = 0;
    ResultSet  rslt          = null;
    Statement  stmt          = null;

    System.out.println(sql);
    try {
      stmt = conn.createStatement();
      ((OracleStatement)stmt).defineColumnType(1, Types.VARCHAR);
      ((OracleStatement)stmt).defineColumnType(2, Types.VARCHAR);
      ((OracleStatement)stmt).defineColumnType(3, Types.TIMESTAMP);
      rslt = stmt.executeQuery(sql);
      while (rslt.next()) {
        rows++;
        System.out.print(rslt.getString(1) + "  ");
        System.out.print(rslt.getString(2) + "  ");
        inactive_date = rslt.getDate(3);
        if (inactive_date != null) 
          System.out.println(df.format(inactive_date));
        else
          System.out.println("NULL");
      }
      System.out.println(Integer.toString(rows) + " rows selected");
      System.out.println(" ");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (rslt != null) 
        try { rslt.close(); } catch (SQLException ignore) { }
      if (stmt != null) 
        try { stmt.close(); } catch (SQLException ignore) { }
    }
  
protected voidfinalize()

     if (conn != null) 
       try { conn.close(); } catch (SQLException ignore) { }
     super.finalize();
  
public static voidmain(java.lang.String[] args)

    ExecuteDefinedSelect s = new ExecuteDefinedSelect();

    s.executeDefinedSelect(
     "select code, description, inactive_date " + 
     "from   PERSON_IDENTIFIER_TYPE " + 
     "order by code");