FileDocCategorySizeDatePackage
UsingInstanceOf.javaAPI DocExample4406Fri Dec 01 14:28:18 GMT 2000None

UsingInstanceOf

public class UsingInstanceOf extends Object

Fields Summary
Connection
conn
Constructors Summary
public UsingInstanceOf()

  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
protected voidfinalize()

  if (conn != null) { try { conn.close(); } catch (SQLException ignore) { } }
  super.finalize();
 
private java.lang.StringformatColumn(java.lang.Object object, int displaySize)

  String value  = "";
  if      (object instanceof java.lang.String) {
   value = rpad((String)object, displaySize, ' ");
  }
  else if (object instanceof java.math.BigDecimal) {
   BigDecimal n = (BigDecimal)object;
   value = lpad(n.toString(), 9, ' ");
  }
  else if (object instanceof java.sql.Timestamp) {
   Timestamp ts = (Timestamp)object;
   value = rpad(ts.toString(), 21, ' ");
  }
  else if (object == null) {
   value = rpad(value, displaySize, ' ");
  }
  else {
   System.err.println("Unsupported class: " + object.getClass().getName());
  }
  return value + " ";
 
private java.lang.StringformatHeading(java.lang.Object object, java.lang.String label, int displaySize)

  int    length = displaySize;
  String value  = "";
  if (label != null) {
   value = label;
   if      (object instanceof java.lang.String) {
   }
   else if (object instanceof java.math.BigDecimal) {
    length = 9;
   }
   else if (object instanceof java.sql.Timestamp) {
    length = 21;
   }
   else if (object == null) {
   }
   else {
    System.err.println("Unsupported class: " + object.getClass().getName());
   }
  }
  return rpad(value, length, ' ") + " ";
 
private java.lang.Stringlpad(java.lang.String in, int length, char pad)

  StringBuffer out   = new StringBuffer(length);
  int          least = in.length();
  if (least > length) 
   least = length;
  out.append(in.substring(0, least));
  int          fill  = length - out.length();
  for (int i=0;i < fill;i++) { 
   out.insert(0, pad);
  }
  return out.toString();
 
public static voidmain(java.lang.String[] args)

  UsingInstanceOf tmd = new UsingInstanceOf();
  tmd.process("select to_char( NULL ) a_char, " + 
   "to_date( NULL ) a_date, to_number( NULL ) a_number from sys.dual");
  tmd.process("select 'ABCDEFG' a_char, sysdate a_date, " + 
   "1 an_integer, 1.1 a_float from sys.dual");
  tmd.process("delete PERSON where 1 = 0");
 
public voidprocess(java.lang.String sql)

  int               columns = 0;
  int               i       = 0;
  int               rows    = 0;
  ResultSet         rslt    = null;
  ResultSetMetaData meta    = null;
  Statement         stmt    = null;
  try {
   stmt = conn.createStatement();
   if (stmt.execute(sql)) {
    rslt = stmt.getResultSet();
    meta = rslt.getMetaData();
    columns = meta.getColumnCount();
    while (rslt.next()) {
     rows++;
     if (rows == 1) {
      for (i = 1;i <= columns;i++) {
       System.out.print(
        formatHeading(
         rslt.getObject(i), 
         meta.getColumnLabel(i), 
         meta.getColumnDisplaySize(i)));  
      }
      System.out.println("");
     }
     for (i = 1;i <= columns;i++) {
      System.out.print(
       formatColumn(
        rslt.getObject(i), 
        meta.getColumnDisplaySize(i)));  
     }
     System.out.println("");
    }
    System.out.println("");
   }
   else {
    rows = stmt.getUpdateCount();
    System.out.println(Integer.toString(rows) + " rows affected");
    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) { }
  }
 
private java.lang.Stringrpad(java.lang.String in, int length, char pad)

  StringBuffer out   = new StringBuffer(length);
  int          least = in.length();
  if (least > length) 
   least = length;
  out.append(in.substring(0, least));
  int          fill  = length - out.length();
  for (int i=0;i < fill;i++) { 
   out.append(pad);
  }
  return out.toString();