FileDocCategorySizeDatePackage
ExecuteSelect.javaAPI DocExample2110Thu Jun 14 17:42:28 BST 2001None

ExecuteSelect.java

import java.io.*;
import java.sql.*;
import java.text.*;

public class ExecuteSelect {
  Connection conn;

  public ExecuteSelect() {
    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();
    }
  }

  public static void main(String[] args) 
   throws Exception, IOException {
    ExecuteSelect s = new ExecuteSelect();
  
    s.executeSelect(
     "select code, description, inactive_date " + 
     "from   PERSON_IDENTIFIER_TYPE " + 
     "order by code");
  }

  public void executeSelect(String sql) 
   throws IOException, SQLException {
    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();
      rslt = stmt.executeQuery(sql);
      while (rslt.next()) {
        rows++;
        System.out.print(rslt.getString("code") + "  ");
        System.out.print(rslt.getString("description") + "  ");
        inactive_date = rslt.getDate("inactive_date");
        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 void finalize() 
  throws Throwable {
   if (conn != null) 
     try { conn.close(); } catch (SQLException ignore) { }
   super.finalize();
  }
}